首页 > 解决方案 > 在 UIView.animate 期间忽略 withDuration

问题描述

当点击按钮时,我正在尝试为 AVPlayerLayer 的不透明度设置动画。这是我的功能:

@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
    }

启动了不透明动画,但速度非常快(大约 0.5 秒)。我尝试将持续时间更改为 10s 并且动画是相同的

我试图self.view.layoutIfNeeded()在动画块内添加没有效果。

你有什么想法吗?谢谢 !

标签: iosswiftanimation

解决方案


不要为 of 设置动画,而是opacity尝试avPlayerLayer为要添加alpha的位置设置动画,即customViewavPlayerLayer

@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.layoutIfNeeded()


推荐阅读