简体   繁体   English

如何在我自己的应用程序中实现 Apple Music 中的迷你播放器?

[英]How can I implement the miniplayer in Apple Music in my own app?

Apple introduced a new UI in Apple Music on iOS 8.4. Apple 在 iOS 8.4 上的 Apple Music 中引入了新的 UI。

When an audio track begins to play, a miniplayer appears just above the tab bar, and it can be brought into full screen mode my dragging it up or tapping on it.当一个音轨开始播放时,一个迷你播放器会出现在标签栏的正上方,通过向上拖动或点击它可以进入全屏模式。

I imagine this is using the UIViewController containment APIs.我想这是使用 UIViewController 包含 API。 Where can I start with my own implementation?我可以从哪里开始我自己的实现?

How might I implement something similar?我该如何实现类似的东西?

Funnily enough, I came across a framework that implemented this earlier this week.有趣的是,我在本周早些时候遇到了一个框架,它实现了这个。 Give LNPopupController a look.看看LNPopupController It implements the behavior you're looking for (showing a mini bar, then tapping or dragging to present a view controller).它实现了您正在寻找的行为(显示一个迷你栏,然后点击或拖动以显示一个视图控制器)。 If it doesn't quite fit your needs, maybe it can at least provide a starting point for you to implement your own thing.如果它不太适合您的需求,也许它至少可以为您实现自己的东西提供一个起点。

Add a container view in your root view controller, for Apple Music uiTabBar controller.在您的根视图控制器中添加一个容器视图,用于 Apple Music uiTabBar控制器。

func configureContainer() {
        
        // add container
        containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(containerView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor),
            containerView.heightAnchor.constraint(equalToConstant: 64.0)
        ])
        
        // add child view controller view to container
        miniPlayer = MiniPlayerViewController()
        guard let miniPlayer = miniPlayer else { return }
        addChild(miniPlayer)
        miniPlayer.view.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(miniPlayer.view)
        
        
        // Create and activate the constraints for the child’s view.
        guard let miniPlayerView = miniPlayer.view else { return }
        NSLayoutConstraint.activate([
            miniPlayerView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            miniPlayerView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            miniPlayerView.topAnchor.constraint(equalTo: containerView.topAnchor),
            miniPlayerView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
        ])
        
        miniPlayer.didMove(toParent: self)
    }

In my case, I want to hide miniPlayer in one specific VC, so I use NotificationCenter to do it.就我而言,我想将miniPlayer隐藏在一个特定的 VC 中,因此我使用 NotificationCenter 来做到这一点。 Register a notification in root viewController.在根视图控制器中注册通知。

NotificationCenter.default.addObserver(self, selector: #selector(miniPlayerVisibilityNeedsChange(notify:)), name: NSNotification.Name("miniPlayerVisibilityNeedsToBeUpdate"), object: nil)

Post it in your specific VC, that's it!将其发布在您的特定 VC 中,就是这样!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // hide miniPlayer for this vc
        NotificationCenter.default.post(name: NSNotification.Name("miniPlayerVisibilityNeedsToBeUpdate"), object: false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // show miniPlayer again when leaving this vc
        NotificationCenter.default.post(name: NSNotification.Name("miniPlayerVisibilityNeedsToBeUpdate"), object: true)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 swift 的苹果音乐应用程序中实现侧面字母视图? 用于按字母顺序对列表中的项目进行排序 - How can I implement the side alphabetic view in apple's music app in swift? Which used to sort the items in the list in alphabetic order 如果我可以为苹果智能手表开发应用程序来获取原始 PPG 数据并实现我自己的心率检测算法 - If I can develop app for apple smartwatch to get raw PPG data and implement my own algorithm for hear rate detection 人们如何使用我自己创建的IOS应用程序? 我的苹果没有付费帐户 - How can people use my own created IOS app? I have no paid account in apple 如何在其Clock应用程序中实现Apple的“ Snooze”功能? - How can I implement Apple's “Snooze” functionality in their Clock app? 如何为自定义UIWindow实现我自己的getter方法? - How can I implement my own getter method for a custom UIWindow? 如何实现自己的自定义MFMailComposeView? - How can I implement my own custom MFMailComposeView? 从我的iOS应用程序打开Apple Music - Opening Apple Music from my iOS app 苹果可以将流音乐作为独立应用程序观看吗 - can Apple watch stream music as standalone app 如何确定Apple并未篡改我的应用程序? - How can I be sure Apple hasn't tampered with my app? 如何将ios应用程序上传到Apple Store? - How can I upload my ios app to apple store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM