我正在尝试通过屏幕触摸来控制动画
当我触摸屏幕时,视图的 alpha 变为 0
但是如果在 alpha 变为 0 时再次触摸
然后 alpha 再次变为 1(使 alpha 值为 0 的中断动画)
所以我写
class MainViewController: UIViewController {
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
var isHiding:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
showAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 1
})
hideAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 0
})
showAnimation.isUserInteractionEnabled = true
showAnimation.isInterruptible = true
hideAnimation.isUserInteractionEnabled = true
hideAnimation.isInterruptible = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isHiding = !isHiding
if self.isHiding {
self.hideAnimation.startAnimation()
self.showAnimation.stopAnimation(true)
}else{
self.hideAnimation.stopAnimation(true)
self.showAnimation.startAnimation()
}
}
}
但 touchesBegan 仅在动画块完成后调用
我怎么解决这个问题