Sri*_*aju 2 animation core-animation objective-c uiview ios
我有10个UILabels,也许有些UIImageViews.我希望用1个UIView接一个的平滑FadeOut和FadeIn过渡显示所有这些.
我知道将UIView动画放在for循环中将不起作用,因为动画是异步完成的,并且不会产生适当的效果.所以我通常这样做的方法是将UIView动画链接在一起.即在一个元素动画完成后开始下一个.对于3-4个元素,代码看起来没问题.像这样 -
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{ //do something with alpha here - first element }
completion:^(BOOL finished){
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{ //do something with alpha here - 2nd element}
completion:^(BOOL finished){ ... }
}
Run Code Online (Sandbox Code Playgroud)
但对于10多个元素,它会变得非常混乱.怎么会这样做呢?基本上我正在创建一个UIView循环内容,就像一个小部件.
编辑NSTimer而不是循环.
counter 是标题中定义的ivar.
- (void)viewDidLoad
{
[super viewDidLoad];
counter = 0;
[NSTimer scheduledTimerWithTimeInterval:0.30
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
}
- (void) timerTick:(NSTimer *)timer{
UIView *currentView = [self.view.subviews objectAtIndex:counter];
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{ currentView.alpha = 1.0;}
completion:^(BOOL finished){
[UIView animateWithDuration:0.25 animations:^{currentView.alpha = 0.0;}];
}
];
counter++;
if (counter >= [self.view.subviews count]) {
counter = 0;
}
}
Run Code Online (Sandbox Code Playgroud)