4

当一个视图由点击主视图的手势识别器动画时:

-(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);
                         }];
                     }
     ]; 
}

(有链式动画)。如果它是动画并且再次点击主视图,我实际上看到完成处理程序被调用了两次,第一次是 TRUE,第二次是 FALSE。我以为它只会被调用一次,FALSE?我在Apple 的文档中找不到它。如果动画在已经动画化时开始,是否有关于它如何工作的规则?(我认为它适用于相同的视图再次被动画化,并且如果 view2 被动画化而 view1 被动画化则不适用?)


更新:以下代码可以显示更多洞察力:

-(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 %i   y is %i", finished, y);
                         [UIView animateWithDuration:3 animations:^{
                             self.circleView.center = CGPointMake(250, y);
                         }
                          ];

                     }

     ];
    NSLog(@"finished the method call");

}

除了下面@Kai的回答之外,当已经有动画在进行时,似乎对于同一个UIView对象的新动画有一个规则:旧动画将立即完成其效果,并运行新动画,但随后接下来用completion调用旧动画NO,现在开始第 3 个动画,这导致动画 2 完成效果,但接下来completion用 a 调用它的块NO,它使动画 3 立即生效......我们看到动画 4 运行 3 秒。

上面的示例代码可以试一试...为了简化它,只需删除completion块,然后尝试,它证实了规则说:如果我们在同一个对象上开始一个新动画,旧动画生效立即,并运行新的动画......

并且对于该completion块,如果该completion块开始另一个动画,它会变得非常复杂......

所以我认为最后的事情是:是否有任何文档或规范指定这种行为?

4

1 回答 1

1

我认为会发生以下情况:

Your first trigger sets the center to (100,300) and than animates the view (note that the center property changes before you actually see it!). Your second trigger returns immediately (calls completion with YES), because there is nothing to animate (the property's already been set to the very same value before) and resets the center and by that forces the first animation (still running) to stop with NO, because the circleview is teared out from it's disired animation by getting a new center (note that the 2nd trigger does not disturb the 1st animation before calling completion, because the circleview's center property is not changed).

于 2012-06-04T13:08:21.290 回答