gpt4 book ai didi

ios - 如何在触发新动画时取消以前的动画?

转载 作者:IT王子 更新时间:2023-10-29 05:14:03 25 4
gpt4 key购买 nike

我正在编写相机应用,但在用户点击屏幕时无法显示对焦框。

我的代码是(快速):

self.focusView.center = sender.locationInView(self.cameraWrapper)
self.focusView.transform = CGAffineTransformMakeScale(2, 2)
self.focusView.hidden = false

UIView.animateWithDuration(0.5, animations: { [unowned self] () -> Void in
self.focusView.transform = CGAffineTransformIdentity
}, completion: { (finished) -> Void in
UIView.animateWithDuration(0.5, delay: 1.0, options: nil, animations: { () -> Void in
self.focusView.alpha = 0.0
}, completion: { (finished) -> Void in
self.focusView.hidden = true
self.focusView.alpha = 1.0
})
})

但是,如果在上一个动画未完成时连续点击屏幕,新旧动画会混淆,焦点 View 会表现得很奇怪,例如它会很快消失。

谁能告诉我如何取消之前的动画,尤其是之前的完成 block ?

最佳答案

你可以使用方法removeAllAnimations来停止动画
将您的代码替换为以下

self.focusView.center = sender.locationInView(self.cameraWrapper)
self.focusView.transform = CGAffineTransformMakeScale(2, 2)
self.focusView.hidden = false
self.focusView.layer.removeAllAnimations() // <<==== Solution
UIView.animateWithDuration(0.5, animations: { [unowned self] () -> Void in
self.focusView.transform = CGAffineTransformIdentity
}, completion: { (finished) -> Void in

UIView.animateWithDuration(0.5, delay: 1.0, options: nil, animations: { () -> Void in
self.focusView.alpha = 0.0
}, completion: { (finished) -> Void in
self.focusView.hidden = true
self.focusView.alpha = 1.0
})
})


引用:link
enter image description here

关于ios - 如何在触发新动画时取消以前的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com