如何在自己的应用程序中实现Apple Music中的miniplayer?

时间:2015-08-27 23:56:35

标签: ios uiview uiviewcontroller uigesturerecognizer apple-music

Apple在iOS 8.4上推出了Apple Music中的新UI。

当音轨开始播放时,标签栏上方会出现一个迷你图层,可以将其拖动到全屏模式,也可以将其拖动或点击它。

我想这是使用UIViewController包含API。我在哪里可以从我自己的实现开始?

我如何实现类似的东西?

2 个答案:

答案 0 :(得分:11)

有趣的是,我遇到了一个本周早些时候实现的框架。给LNPopupController看一看。它实现了您正在寻找的行为(显示迷你条,然后点击或拖动以呈现视图控制器)。如果它不太适合您的需求,也许它至少可以为您实现自己的事情提供一个起点。

答案 1 :(得分:0)

在根视图控制器中为 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)
    }

就我而言,我想在一个特定的 VC 中隐藏 miniPlayer,因此我使用 NotificationCenter 来实现。在根视图控制器中注册通知。

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

将其发布到您的特定 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)
    }