我想为我的应用程序创建一个登录页面,在这种情况下,我想使用视频作为登录页面的背景,例如 vine、spotify、uber... 我播放视频没有问题,并在 Storyboard中添加一些按钮和标签到我的 View Controller ,运行应用程序后我看不到 View Controller 的对象 我只是看到视频和隐藏在视频背景后面的对象如何解决这个问题,,, 谢谢你的帮助
这是我播放视频的代码,,,
lazy var playerLayer:AVPlayerLayer = {
let player = AVPlayer(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
player.muted = true
player.allowsExternalPlayback = false
player.appliesMediaSelectionCriteriaAutomatically = false
var error:NSError?
// This is needed so it would not cut off users audio (if listening to music etc.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch var error1 as NSError {
error = error1
} catch {
fatalError()
}
if error != nil {
print(error)
}
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
playerLayer.backgroundColor = UIColor.blackColor().CGColor
player.play()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidReachEnd", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
return playerLayer
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.layer.addSublayer(self.playerLayer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
playerLayer.frame = self.view.frame
}
func playerDidReachEnd(){
self.playerLayer.player!.seekToTime(kCMTimeZero)
self.playerLayer.player!.play()
}
最佳答案
您需要在 Storyboard加载时创建的所有 View 下方添加您的 playerLayer
。替换
self.view.layer.addSublayer(self.playerLayer)
与
self.view.layer.insertSublayer(self.playerLayer, atIndex:0)
应该完成这项工作,除非您确实希望在视频层后面有任何 View Controller View 的 subview 。
关于ios - View Controller 的对象 swift 隐藏在视频背景后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041354/