[英]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.