給UIButton擴展方法
extension UIButton {
func customBtn(customTitle title: String) {
setTitle(title, for: .normal)
setTitleColor(.white, for: .normal)
layer.cornerRadius = 5.0
layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 1.0
}
}
調(diào)用:
loginBtn.customBtn(customTitle: "登錄")
regisBtn.customBtn(customTitle: "注冊")
AVPlayerViewController的使用
實例化
let playerVC = AVPlayerViewController()
屬性和監(jiān)聽:
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "moments", ofType: "mp4")!)
playerVC.player = AVPlayer(url: url)//指定播放源
playerVC.showsPlaybackControls = false//是否顯示工具欄
playerVC.videoGravity = AVLayerVideoGravityResizeAspectFill//視頻畫面適應(yīng)方式
playerVC.view.frame = YHRect
playerVC.view.alpha = 0
//監(jiān)聽視頻播放完的狀態(tài)
NotificationCenter.default.addObserver(self, selector: #selector(repeatPlay), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerVC.player?.currentItem)
view.addSubview(playerVC.view)
view.sendSubview(toBack: playerVC.view)//放到最底層
UIView.animate(withDuration: 1) {
self.playerVC.view.alpha = 1;
self.playerVC.player?.play()
}
重復(fù)播放:
playerVC.player?.seek(to: kCMTimeZero)
playerVC.player?.play()
<li><h1>學(xué)習(xí)代碼:<h1></li>
BtnExtensions.swift
import Foundation
import UIKit
extension UIButton {
func customBtn(customTitle title: String) {
setTitle(title, for: .normal)
setTitleColor(.white, for: .normal)
layer.cornerRadius = 5.0
layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 1.0
}
}
ViewController.swift
import UIKit
import AVKit
import MediaPlayer
let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width
let loginBtnRect = CGRect(x: 30, y: YHHeight-150, width: YHWidth-60, height: 50)
let regisBtnRect = CGRect(x: 30, y: YHHeight-75, width: YHWidth-60, height: 50)
class ViewController: UIViewController, AVPlayerViewControllerDelegate {
let playerVC = AVPlayerViewController()
let loginBtn = UIButton(frame: loginBtnRect)
let regisBtn = UIButton(frame: regisBtnRect)
override func viewDidLoad() {
super.viewDidLoad()
setupView()
setMoviePlayer()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func setMoviePlayer() {
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "moments", ofType: "mp4")!)
playerVC.player = AVPlayer(url: url)//指定播放源
playerVC.showsPlaybackControls = false//是否顯示工具欄
playerVC.videoGravity = AVLayerVideoGravityResizeAspectFill//視頻畫面適應(yīng)方式
playerVC.view.frame = YHRect
playerVC.view.alpha = 0
//監(jiān)聽視頻播放完的狀態(tài)
NotificationCenter.default.addObserver(self, selector: #selector(repeatPlay), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerVC.player?.currentItem)
view.addSubview(playerVC.view)
view.sendSubview(toBack: playerVC.view)//放到最底層
UIView.animate(withDuration: 1) {
self.playerVC.view.alpha = 1;
self.playerVC.player?.play()
}
}
//回到起點,重新播放
func repeatPlay() {
playerVC.player?.seek(to: kCMTimeZero)
playerVC.player?.play()
}
func setupView() {
view.backgroundColor = .white
loginBtn.customBtn(customTitle: "登錄")
regisBtn.customBtn(customTitle: "注冊")
loginBtn.addTarget(self, action: #selector(buttonTap(_:)), for: .touchUpInside)
regisBtn.addTarget(self, action: #selector(buttonTap(_:)), for: .touchUpInside)
view.addSubview(loginBtn)
view.addSubview(regisBtn)
}
func buttonTap(_ sender: UIButton) {
print("點擊的按鈕:"+sender.currentTitle!)
}
}