4

我有 3 个UILabels我想淡出,几秒钟后一个接一个。我的问题是这些都是同时发生的。我正在尝试链接动画,但我无法让它工作。我尝试了各种建议,但无济于事。我知道这不可能这么难。我最好将它们捆绑在一个动画方法中,因为我想animationDidStop在显示所有 3 个标签之后触发其他功能。有什么帮助或建议吗??

这是我的代码:

- (void)viewDidLoad
{
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblReady];
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblSet];
    [self fadeAnimation:@"fadeAnimation" finished:YES target:lblGo];
}


- (void)fadeAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
    [UIView beginAnimations:nil context:nil];
    [UIView beginAnimations:animationID context:(__bridge void *)(target)];
    [UIView setAnimationDuration:2];

    [target setAlpha:0.0f];
    [UIView setAnimationDelegate:self];    
    [UIView commitAnimations];
}
4

2 回答 2

8

UIView使用最新的动画方法会更容易:

[UIView animateWithDuration:2.0 animations:^ {
    lblReady.alpha = 0;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:2.0 animations:^ {
        lblSet.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^ {
            lblGo.alpha = 0;
        } completion:^(BOOL finished) {
            // Add your final post-animation code here
        }];
    }];
}];
于 2013-03-20T03:43:28.453 回答
1

你应该让他们 performSelector:withObject:afterDelay: 代替。

因此,将您的代码更改为:

- (void)viewDidLoad
{
    [self performSelector:@selector(fadeAnimation:) withObject:lblReady afterDelay:0];
    [self performSelector:@selector(fadeAnimation:) withObject:lblSet afterDelay:2];
    [self performSelector:@selector(fadeAnimation:) withObject:lblGo afterDelay:4];
}

-(void)fadeAnimation:(UIView *)target {
    [self fadeAnimation:@"fadeAnimation finished:YES target:target];
}




- (void)fadeAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
    [UIView beginAnimations:nil context:nil];
    [UIView beginAnimations:animationID context:(__bridge void *)(target)];
    [UIView setAnimationDuration:2];

    [target setAlpha:0.0f];
    [UIView setAnimationDelegate:self];    
    [UIView commitAnimations];
}

这只是在 0、2 或 4 个部分之后调用每个操作代码。如果动画持续时间改变了,这些数字也应该相应地改变。

如果你想使用块动画而不是使用旧的动画风格,你可以这样做:

[UIView animateWithDuration:2 animations ^{
    //put your animations in here
} completion ^(BOOL finished) {
    //put anything that happens after animations are done in here.
    //could be another animation block to chain animations
}];
于 2013-03-20T03:41:27.850 回答