2011-07-13 36 views
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. 框架通过状态1从状态0到动画状态2
  2. 帧动画从状态0直接到状态2,忽略状态1

我期望结果#2至因为我认为动画提交时会记录属性的状态。我在我的应用程序中看到一个行为,似乎表明结果#1正在发生,因此我的问题。

回答

1

实际上回答为3以上皆非。

动画块将只动画该属性的最后状态变化。所以在你的情况下,框架将从状态1动画到状态2.

+0

有趣的答案。如果是这种情况,第一次状态变化发生在图书馆内某处,我的动画注定会看起来很糟糕? –

+0

不一定。根据库的功能,您可以在自己的beginAnimation/commitAnimation事务中包装第一个状态更改。在该事务中,您可以调用setAnimationDelegate:和setAnimationDidStopSelector:来标识第一个动画完成后应该调用的方法。在该方法中,您可以触发第二个状态转换的动画。 – cduhn

+0

理想情况下,我想完全忽略第一个状态转换。这实际上是修改另一个属性造成的连锁效应,它导致从0到2的期望转换看起来很糟糕。有任何想法吗? –

1

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

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我正在用动画块中调用的循环方法构建新的视图位置)。