IOS:alpha问题

时间:2011-08-09 15:01:29

标签: ios cocoa-touch core-animation

我有这段代码,其中successview以alpha = 0.00开头

- (void) startAnimation{
    //immediately
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:1.00];
    [UIView commitAnimations];
    //in three seconds
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:0.00];
    [UIView commitAnimations];
}

这样,在第一个动画中(alpha 0.00到1.00),它不会在3秒内发生,而是立即发生,而在第二个动画中(alpha 1.00到alpha 0.00)它会在3秒内发生

如果我只写第一个动画:

- (void) startAnimation{
    //in three seconds
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [successView setAlpha:1.00];
    [UIView commitAnimations];
}

它发生在3秒内,为什么在最好的例子中它不会发生?

2 个答案:

答案 0 :(得分:0)

您可以尝试为第二个动画设置动画延迟(请注意,如果您的目标是iOS4.0 +,则不建议使用此API,并且您应该使用基于块的animateWithDuration:delay:options:animations:completion:动画):

...
[UIView setAnimationDelay:3.0]; 
...

但是,如果您只想将视图的alpha更改为1并将其还原,则可以仅使用第1个动画但将其自动反转:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[UIView setAnimationRepeatAutoreverses:YES];
[successView setAlpha:1.00];
[UIView commitAnimations];

答案 1 :(得分:0)

- (void)startAnimation
{
    [UIView beginAnimations:@"successAnimationPart1" context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationDelegate:self];
    [successView setAlpha:1.00];
    [UIView commitAnimations];
}

并添加此动画委托方法:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if ([animationID isEqualToString:@"successAnimationPart1"]) {
        // when first animation is done, call second
        [UIView beginAnimations:@"successAnimationPart2" context:nil];
        [UIView setAnimationDuration:3];
        [UIView setAnimationDelegate:self];
        [successView setAlpha:0.00];
        [UIView commitAnimations];
    }
}
相关问题