I have this Code:
-(void)animationLoop{
CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
[UIView animateWithDuration:1.0
animations: ^{ myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [self randomFloatingGenerator], myCircleUIView.frame.origin.y + [self randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height); }
completion:
^(BOOL finished) {
[UIView animateWithDuration:1.0
animations:^{ myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);}
completion:
^(BOOL finished) {[self animationLoop];}];
}];
}
But I am trying to stop the animation when interacting with it, but [myCircleUIView.layer removeAllAnimations];
won't do the job, any suggestions?
Add bool in your loop and set false when need to stop animation as show in code below...
}]; }
Just set
To stop animation
When you are stopping animation with
CALayer's
-removeAllAnimations
completion callback is called withfinished == NO
. So change your animation code like this:I also advise you not to pass strong references to
self
to blocks that are copied to heap if you don't really want to because of possibleretain cycle
.