[英]animating a CALayer
我在UIView上有一個CALayer。
UIView的動畫正是我想要它,但是這是在它之上的PlayerView似乎“重生”離屏幕動畫,然后到了UIView中開始,然后它會以動畫的UIViews幀中的位置。
我想知道是否有CALayers的任何屬性可以自動處理此問題,但我什么都沒找到。
這是此代碼:
let window = UIApplication.shared.keyWindow!
v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
let v2 = UIView(frame: .zero)
window.addSubview(v!);
window.addSubview(v2)
v?.backgroundColor = UIColor.black
v2.backgroundColor = UIColor.clear
v?.layer.cornerRadius = (v?.frame.width)! * 0.025
guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
playerLayer = AVPlayerLayer(player: player)
// playerLayer.frame = playerView.frame
playerLayer?.frame = (playerView?.bounds)!
playerLayer?.masksToBounds = true
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
v?.layer.addSublayer(playerLayer!)
player?.play()
player?.isMuted = true
//looper
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil)
{ notification in
let t1 = CMTimeMake(5, 100)
self.player?.seek(to: t1)
self.player?.play()
}
let superview = playerView?.superview
v2.frame = (v2.convert((playerView?.frame)!, from: superview))
v?.frame = (v?.convert((playerView?.frame)!, from: superview))!
window.addConstraintsWithFormat("H:|[v0]|", views: v!)
window.addConstraintsWithFormat("V:|[v0]|", views: v!)
playerLayer?.frame = (v?.frame)!
self.playerLayer?.frame = (self.v?.frame)!
UIView.animate(withDuration: 4, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.window?.layoutIfNeeded()
}, completion: { (completed) in
self.playerLayer?.frame = (self.v?.frame)!
})
}
有什么建議么?
編輯:
我已包含更新的代碼
let window = UIApplication.shared.keyWindow!
v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
let v2 = UIView(frame: .zero)
window.addSubview(v!);
v?.addSubview(playerView2!)
window.addSubview(v2)
v?.backgroundColor = UIColor.black
v2.backgroundColor = UIColor.clear
v?.layer.cornerRadius = (v?.frame.width)! * 0.025
playerView2?.layer.cornerRadius = (playerView2?.bounds.width)! * 0.025
guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
playerLayer = AVPlayerLayer(player: player)
// playerLayer.frame = playerView.frame
playerLayer?.frame = (playerView2?.bounds)!
playerLayer?.masksToBounds = true
playerLayer?.cornerRadius = (playerView2?.bounds.width)! * 0.025
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
self.playerView2?.layer.addSublayer(playerLayer!)
// player?.play()
player?.isMuted = true
//looper
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: nil)
{ notification in
let t1 = CMTimeMake(5, 100)
self.player?.seek(to: t1)
// self.player?.play()
}
self.playerView2?.frame = (self.v?.bounds)!
self.playerLayer?.frame = (self.playerView?.bounds)!
UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.v?.frame = window.frame
self.playerView2?.frame = (self.v?.frame)!
self.playerLayer?.frame = (self.playerView2?.frame)!
self.window?.layoutIfNeeded()
}, completion: { (completed) in
})
}
設置playerLayer.frame
時,您將獲得一個隱式動畫。
如果您不想使用任何動畫,請暫時禁用如下所示的隱式動畫:
UIView.animate(withDuration: 4, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.window?.layoutIfNeeded()
}, completion: { (completed) in
let priorValue = CATransaction.disableActions()
CATransaction.setDisableActions(true)
self.playerLayer?.frame = self.v!.frame
CATransaction.setDisableActions(priorValue)
})
如果您想為playerLayer.frame
設置動畫,那么最好的方法是創建一個使用AVPlayerLayer
作為其圖層的UIView
子類,如下所示:
class PlayerView: UIView {
override class var layerClass: AnyClass { return AVPlayerLayer.self }
private(set) lazy var playerLayer: AVPlayerLayer = { self.layer as! AVPlayerLayer }()
}
然后,您可以使用PlayerView
代替裸露的AVPlayerLayer
並使用UIKit動畫。 由於您無法直接初始化AVPlayerLayer
( UIView
使用默認的初始化程序創建其圖層),因此需要像這樣設置PlayerView
的播放器:
playerView?.playerLayer = player
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.