Reputation: 65
I'am trying to animate the opacity of a AVPlayerLayer when a button is tapped. Here is my function :
@IBAction func boutonTapped(_ sender: UIButton) {
if(paused){
UIView.animate(withDuration: 5.0, animations: {
self.avPlayerLayer.opacity = 1.0
}, completion: nil)
//avPlayer.play()
}else{
UIView.animate(withDuration: 5.0, animations: {
self.avPlayerLayer.opacity = 0
}, completion:nil)
//avPlayer.pause()
}
paused = !paused
}
An opacity animation is launched but it is very speed (about 0.5s). I tried to change the duration for 10s and the animation is the same
I tried to add self.view.layoutIfNeeded()
inside the animation block with no effect.
Have you any idea ? Thanks !
Upvotes: 1
Views: 170
Reputation: 3157
I think you need to put self.view.layoutIfNeeded() inside animation block but not the opacity update code. Like this,
@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
}
Upvotes: 0
Reputation: 5823
You should change an animation code to change opacity of AVPlayerLayer
as follow:
@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 is view where you have added AVPlayerLayer
instance.
Upvotes: 0
Reputation: 24341
Instead of animating the opacity
of avPlayerLayer
, try animating the alpha
of the customView
where you're adding avPlayerLayer
, i.e.
@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
}
There is no need to call self.view.layoutIfNeeded()
.
Upvotes: 1