我有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];
}
答案 0 :(得分: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
}];
}];
}];
答案 1 :(得分: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
}];