I use the following approach:
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; //set the animating flag animating = YES; //animation 1 [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{ view.frame = CGRectMake(0, 100, 200, 200); } completion:^(BOOL finished){ //stops the chain if(! finished) return; //animation 2 [UIView animateWithDuration:2 delay:0 options: UIViewAnimationOptionRepeat |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{ [UIView setAnimationRepeatCount:1.5]; view.frame = CGRectMake(50, 100, 200, 200); } completion:^(BOOL finished){ //stops the chain if(! finished) return; //animation 3 [UIView animateWithDuration:2 delay:0 options:0 animations:^{ view.frame = CGRectMake(50, 0, 200, 200); } completion:nil]; }]; }]; - (void)stop { animating = NO; [view.layer removeAllAnimations]; }
The removeAllAnimations message immediately terminates the animation block and its completion block is called. There the animation flag is checked and the chain is stopped.
Is there a better way to do this?
user263865 Nov 18 2018-11-11T00: 00Z
source share