Ios withDuration在UIView.animate期间被忽略
我正在尝试在点击按钮时设置AVPlayerLayer不透明度的动画。以下是我的功能:Ios withDuration在UIView.animate期间被忽略,ios,swift,animation,Ios,Swift,Animation,我正在尝试在点击按钮时设置AVPlayerLayer不透明度的动画。以下是我的功能: @IBAction func bouton点击(uu发送方:ui按钮){ 如果(暂停){ UIView.animate(持续时间:5.0,动画:{ self.avPlayerLayer.opacity=1.0 },完成日期:无) //avPlayer.play() }否则{ UIView.animate(持续时间:5.0,动画:{ self.avPlayerLayer.opacity=0 },完成日期:无) /
@IBAction func bouton点击(uu发送方:ui按钮){
如果(暂停){
UIView.animate(持续时间:5.0,动画:{
self.avPlayerLayer.opacity=1.0
},完成日期:无)
//avPlayer.play()
}否则{
UIView.animate(持续时间:5.0,动画:{
self.avPlayerLayer.opacity=0
},完成日期:无)
//avPlayer.pause()
}
暂停=!暂停
}
不透明度动画已启动,但速度非常快(约0.5s)。我试图改变持续时间为10秒,动画是一样的
我试图在动画块中添加self.view.layoutifneed()
,但没有效果
你知道吗?谢谢 我认为您需要将self.view.layoutifneed()放在动画块中,而不是放在不透明度更新代码中。像这样,
@IBAction func boutonTapped(_ sender: UIButton) {
if (paused) {
self.avPlayerLayer.opacity = 1.0
//avPlayer.play()
} else {
self.avPlayerLayer.opacity = 0
//avPlayer.pause()
}
UIView.animate(withDuration: 5.0, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
paused = !paused
}
您应该更改动画代码以更改
AVPlayerLayer
的不透明度,如下所示:
@IBAction func boutonTapped(_ sender: UIButton) {
UIView.transition(with: self.videoPlayerView, duration: 5.0, options: [.transitionCrossDissolve], animations: {
self.videoPlayerView.layer.sublayers?.first(where: { $0 is AVPlayerLayer })?.opacity = paused ? 1 : 0
}, completion: nil)
paused ? avPlayer.play() : avPlayer.pause()
}
videoPlayerView是您添加了
AVPlayerLayer
实例的视图。不要为AVPlayerLayer
的不透明度设置动画,试着为正在添加AVPlayerLayer
的customView
的alpha
设置动画,即
@IBAction func boutonTapped(_ sender: UIButton) {
UIView.animate(withDuration: 5.0) {
self.customView.alpha = paused ? 1.0 : 0.0 //here...
paused ? self.avPlayer.play() : self.avPlayer.pause()
}
paused = !paused
}
无需调用self.view.layoutifneed()