1

如果我将一个属性两次设置为动画块内的两个不同值会发生什么?如果我执行以下伪代码:

myView.frame = CGRectMake(0,  0, 50, 50); // state 0
[UIView beginAnimations:@"showBanner" context:NULL];
{
    [UIView setAnimationDuration:2];
    myView.frame = CGRectMake(0, 20, 50, 50); // state 1
    myView.frame = CGRectMake(0, 10, 50, 50); // state 2
}
[UIView commitAnimations];

我应该得到以下哪个结果?

  1. 帧从状态 0 动画到状态 1 到状态 2。
  2. 帧从状态 0 直接动画到状态 2,忽略状态 1。

我希望结果 #2 会发生,因为我认为在提交动画时会记录属性的状态。我的应用程序中出现了一种行为,这似乎表明结果#1 正在发生,因此我提出了问题。

4

3 回答 3

1

(2) 帧从状态 0 直接动画到状态 2,忽略状态 1。

于 2011-07-13T19:22:25.180 回答
1

其实答案是3。以上都不是。

动画块只会动画属性的最后状态变化。因此,在您的情况下,框架将从状态 1 动画到状态 2。

于 2011-07-13T20:41:46.063 回答
0

我需要这个问题的答案,但在这里给出的任何一个都不能令人满意。我建立了一个快速测试,放置了一个像这样的小子视图:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
    self.testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.testView];
}

然后提供了两个这样的替代动画:

- (void)animateWithSimpleBlock {

    [UIView animateWithDuration:2 animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

- (void)animateWithQualifiedBlock {

    [UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

以下是发生的情况:在 上animateWithSimpleBlock,testView 在单帧(无动画)中从它的初始 0,0 位置跳转到中间位置 200,200。从那里,它动画超过 2 秒到最终的 0,300 位置。

但是animateWithQualifiedBlock将 testView 从最初的 0,0 位置平滑地动画到最终的 0,300 位置。这是我需要的行为(b/c 我正在使用动画块中调用的循环方法建立新的视图位置)。

于 2013-06-12T03:44:48.557 回答