iOS-动画未停止,未按预期调用完成方法
我面临着停止动画、阻止调用动画的完成函数并返回真变量的困难 以下代码是我的代码的简化版本,存在相同的问题iOS-动画未停止,未按预期调用完成方法,ios,swift,animation,completion,Ios,Swift,Animation,Completion,我面临着停止动画、阻止调用动画的完成函数并返回真变量的困难 以下代码是我的代码的简化版本,存在相同的问题 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) viewButtonStart.frame = CGRect(x: 10, y: 10, width: 100, h
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
viewButtonStart.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
viewButtonStart.backgroundColor = UIColor.greenColor()
let gesture = UITapGestureRecognizer(target: self, action: #selector(startAnimation))
viewButtonStart.addGestureRecognizer(gesture)
addSubview(viewButtonStart)
viewButtonStop.frame = CGRect(x: 120, y: 10, width: 100, height: 100)
viewButtonStop.backgroundColor = UIColor.yellowColor()
let gesture2 = UITapGestureRecognizer(target: self, action: #selector(stopAnimation))
viewButtonStop.addGestureRecognizer(gesture2)
addSubview(viewButtonStop)
viewShow.backgroundColor = UIColor.blueColor()
viewShow.frame = CGRect(x: screenWidth - 10 - 100, y: 10, width: 100, height: 100)
addSubview(viewShow)
isAnimationRunning = false
startAnimation()
startAnimation()
startAnimation()
}
func startAnimation()
{
print("startAnimation()")
if isAnimationRunning
{
stopAnimation()
}
// (*1) More data handling here...
isAnimationRunning = true
UIView.animateKeyframesWithDuration(
5,
delay: 0,
options: UIViewKeyframeAnimationOptions.CalculationModeLinear,
animations: animations(),
completion: completion())
}
func stopAnimation()
{
self.viewShow.layer.removeAllAnimations()
}
func animations() -> (() -> Void)
{
return {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1, animations: {
self.viewShow.alpha = 1
})
}
}
func completion() -> ((Bool) -> Void)?
{
return { (completed: Bool) in
print("animation completed(\(completed))")
if completed
{
self.isAnimationRunning = false
// (*2) More completion handling here....
}
}
}
每次动画启动时,它都会检查并停止动画是否正在运行。当我在init函数中调用startAnimation()3次时,前两个动画应该停止,但不会停止。即使是最糟糕的情况,动画完成后也不会立即调用completion函数。只有在UIView.addKeyframeWithRelativeStartTime中的属性未更改时,才会出现此问题。在本例中多次调用startAnimations()可能看起来很奇怪,但在实际情况中,我会根据不同的情况设置并重新启动不同的动画
控制台中显示的实际结果如下:
startAnimation()
startAnimation()
startAnimation()
animation completed(true)
animation completed(true)
animation completed(true)
我希望有这样的东西:
startAnimation()
startAnimation()
animation completed(false)
startAnimation()
animation completed(false)
animation completed(true)
以下结果对我来说也是可以接受的:
startAnimation()
animation completed(true)
startAnimation()
animation completed(true)
startAnimation()
animation completed(true)
or this:
startAnimation()
startAnimation()
startAnimation()
animation completed(false)
animation completed(false)
animation completed(true)
该问题导致(*1)和(*2)中使用的变量不正确
非常感谢您的帮助或建议。最后,我通过检查当前属性是否与动画结束属性相同来解决问题。如果属性没有更改,它将跳过调用动画
这是一个相当肮脏的方法,但在我的情况下它解决了这个问题。如果您有更好的解决方案,我将期待您的帮助。谢谢。最后,我通过检查当前属性是否与动画结束属性相同来解决问题。如果属性没有更改,它将跳过调用动画 这是一个相当肮脏的方法,但在我的情况下它解决了这个问题。如果您有更好的解决方案,我将期待您的帮助,谢谢