In iOS, what happens when a view animation starts when it is already animating?

When the animated view using the gesture recognizer clicks the main view:

-(void) doAnimate { [UIView animateWithDuration:3 animations:^{ self.circleView.center = CGPointMake(100, 300); } completion:^(BOOL finished) { NSLog(@"finished is %i", finished); [UIView animateWithDuration:1 animations:^{ self.circleView.center = CGPointMake(250, 300); }]; } ]; } 

(there is a chain animation). If it is animated and the main view is listened again, I actually see the completion handler twice, first with TRUE, and the second time with FALSE. I thought it should have been called only once, with FALSE? I can not find it in the Apple doc . Is there a rule how it works if an animation starts when it is already animating? (I think this applies, is it the same view that animates again and does not apply if view2 is animated while the animation is view1 animation?)


Update: The following code may display more information:

 -(void) dropAnimate:(UIGestureRecognizer *) g { int n = arc4random() % 10000; int y = 501 + arc4random() % 200; NSLog(@"y is %i", y); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, y, 10, 10)]; label.text = @"x"; [self.view addSubview:label]; [UIView animateWithDuration:3 animations:^{ NSLog(@"n is %i", n); self.circleView.center = CGPointMake(100, y); } completion:^(BOOL finished) { NSLog(@"n is %i", n); NSLog(@"finished is %iy is %i", finished, y); [UIView animateWithDuration:3 animations:^{ self.circleView.center = CGPointMake(250, y); } ]; } ]; NSLog(@"finished the method call"); } 

In addition to @Kai below, it seems that there is a new rule for a new animation for the same UIView object when the animation is already happening: the old animation will immediately end its effect and the new animation will start, but then the next old completion animation is called with using NO , and now the 3rd animation starts, which forces animation 2 to complete the effect, but then its completion block is called, with NO , and this forces animation 3 to take effect immediately ... and we see animation 4 working for 3 seconds.

The above code example can be tried ... and to simplify it, simply delete the completion block and then try it and it will confirm the rule that says: if we start a new animation on the same object, the old animation takes effect immediately, and the new animation starting up ...

And with the completion block, it can become quite complicated if the completion block starts another animation ...

So, I think, finally: do any documents or specification define this behavior?

+4
source share
1 answer

I think the following happens:

Your first trigger sets center to (100,300) and animates the view (note that the center property changes before you see it!). Your second trigger returns immediately (calls completion with YES ), because there is nothing to animate (the property has already been set to the same value before) and resets the center value and thereby causes the first animation to run) to stop with NO. because the circleview disabled animation from it, getting a new center (note that the second trigger does not interfere with the first animation before calling completion , because the circleview center property does not change).

+1
source

Source: https://habr.com/ru/post/1415942/


All Articles