ios - withDuration 在 UIView.animate 期间被忽略

标签 ios swift animation

我正在尝试在点击按钮时为 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 并且动画是相同的

我尝试在动画 block 中添加 self.view.layoutIfNeeded() 但没有效果。

你有什么想法吗?谢谢!

最佳答案

不要为 avPlayerLayeropacity 设置动画,而是尝试为 customViewalpha 设置动画添加avPlayerLayer,即

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

关于ios - withDuration 在 UIView.animate 期间被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920617/

相关文章:

ios - ViewControl 之外的 Swift 选择器

python - Matplotlib: 'module'对象没有属性 'FFMpegWriter'/'Writer'

ios - UITableViewRowAnimation 被忽略

animation - 半旋转导航时 flutter 旋转过渡

android - 视频背景自动播放无法在移动设备 (Wordpress) 上运行

ios - UIView 自动旋转问题

ios - 如何处理高度依赖当前位置的应用程序中的位置拒绝?

ios - 选择 DatePicker Swift 4 的默认值

ios - 仅当触摸类型为手指时,如何将触摸事件传递给其他 View ?

ios - UITableView 中的自定义单元格在滑动后立即移动以进行编辑