I am facing difficulties to stop animations, and prevent the animation's completion function to be called and returned a TRUE variable.
The following codes is a simplified version of my codes which the same issue exists.
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....
}
}
}
For every time the animation starts, it will check and stop if the animation is running. When I call startAnimation() 3 times in init function, the first 2 animations should stop but it don't. Even worst, the completion function does not called immediately when the animation finished. And this issue only appears when the properties in UIView.addKeyframeWithRelativeStartTime is unchanged. Calling startAnimations() a few times in the example may seems unless, but in real case, I will setup and restart different animations according to different situation.
The actual results displayed in console is this:
startAnimation()
startAnimation()
startAnimation()
animation completed(true)
animation completed(true)
animation completed(true)
I expected to have something like this:
startAnimation()
startAnimation()
animation completed(false)
startAnimation()
animation completed(false)
animation completed(true)
The following results are acceptable for me too:
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)
The issue cause variables used in both (*1) & (*2) to be incorrect.
Any help or suggestion is appreciate, thanks