[英]animateWithDuration Animates Once But Not Twice
我在switch
语句中运行了该动画,并执行了预期的操作。 在计时器内每20秒成功调用一次动画。 但是,动画不会在任何时候发生,而是第一次。 在下面的代码中,您将看到我用来尝试突出问题的println
语句。 每个println
语句都会打印,但不会发生动画。 我想念的是什么?
func popAnimation(indexNumber: Int) {
let image = self.overlayImageView[indexNumber]
image.hidden = false
image.alpha = 0.0
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
println("animation ran")
println(image.alpha)
image.image = UIImage(named: "lowCountOverlay")
image.alpha = 1.0
}, completion: { (Bool) -> Void in
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
println("animation 2 ran")
println(image.alpha)
image.alpha = 0.0
}, completion: { (Bool) -> Void in
println("animation 3 ran")
image.hidden = true
})
})
}
更新:如果我从第二个完成块中删除image.hidden
,它可以正常工作并反复进行动画处理。 有趣的是,如果未隐藏它,则它应该覆盖视图中的其他交互式内容,但不是。 其他内容可以在模拟器中完美访问。 UIImageView image
绝对是Storyboard中的顶层。
我可能会离开,但似乎您正在从NSTimer中运行,它可能在主线程中,也可能不在主线程中。 UI更新需要在主线程中进行,尝试将完成处理程序包装在指向主线程的GCD调度中,如下所示,看看是否有帮助? 甚至可以将整个过程强制到主线程。
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
// First animation
}, , completion: { (Bool) -> Void in
dispatch_async(dispatch_get_main_queue(),{
// second animation
})
})
我认为您需要在其他地方寻找错误。 您似乎并没有在代码中遗漏任何东西,因为我能够重复运行动画而没有任何问题。 我已经将您的代码原样复制到一个单独的项目中 ,并且动画每次都能正常工作。
尝试更换:
let image = self.overlayImageView[indexNumber]
image.hidden = false
image.alpha = 0.0
与
let image = self.overlayImageView[indexNumber]
image.superview.bringSubviewToFront(image)
image.hidden = false
image.alpha = 0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.