Building Spotify App in Swift 5 & UIKit - Part 18 - Play Music (Xcode 12, 2021, Swift 5)
static func startPlayback(from viewController: UIViewController, track: AudioTrack) {
let vc = PlayerViewController()
vc.title = track.name
viewController.present(UINavigationController(rootViewController: vc), animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let section = sections[indexPath.section]
switch section {
...
case .recommendedTracks(viewModels: let viewModels):
let track = tracks[indexPath.row]
PlaybackPresenter.startPlayback(from: self, track: track)
}
}
PlaybackPresenter
를 통해 현재 뷰 컨트롤러 및 트랙 데이터를 넘기기import UIKit
class PlayerViewController: UIViewController {
private let imageView: UIImageView = {
// Track Cover View
}()
private let controlsView = PlayerControlsView()
...
}
extension PlayerViewController: PlayerControlsViewDelegate {
// delegate functions to use playerControls
func playerControlsViewDidTapPlayPauseButton(_ playerControllsView: PlayerControlsView) {
}
func playerControlsViewDidTapForwardButton(_ playerControllsView: PlayerControlsView) {
}
func playerControlsViewDidTapBackwardButton(_ playerControllsView: PlayerControlsView) {
}
}
PlayerControlsView
를 통해 별도로 구성PlayerViewController
에서 별도로 사용하기 위한 델리게이트 패턴 import UIKit
protocol PlayerControlsViewDelegate: AnyObject {
func playerControlsViewDidTapPlayPauseButton(_ playerControllsView: PlayerControlsView)
func playerControlsViewDidTapForwardButton(_ playerControllsView: PlayerControlsView)
func playerControlsViewDidTapBackwardButton(_ playerControllsView: PlayerControlsView)
}
final class PlayerControlsView: UIView {
weak var delegate: PlayerControlsViewDelegate?
private let volumeSlider: UISlider = {
// volumeSlider
}()
private let titleLabel: UILabel = {
// titleLabel
}()
private let subtitleLabel: UILabel = {
// subtitleLabel
}()
private let backButton: UIButton = {
// button
}()
private let nextButton: UIButton = {
...
}()
private let playPauseButton: UIButton = {
...
}()
// ... set UI layout
private func setPlayerControlsViewUI() {
...
backButton.addTarget(self, action: #selector(didTapBack), for: .touchUpInside)
playPauseButton.addTarget(self, action: #selector(didTapPause), for: .touchUpInside)
nextButton.addTarget(self, action: #selector(didTapNext), for: .touchUpInside)
}
@objc private func didTapBack() {
}
@objc private func didTapNext() {
}
@objc private func didTapPause() {
}
}