繁体   English   中英

如何链接淡出动画

[英]How to chain fade animations

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

这是我的代码:

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

使用最新的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
        }];
    }];
}];

你应该让他们执行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
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM