iOS的10 UILabels - > 1 UIView的 - >通过与动画循环 [英] iOS 10 UILabels -> 1 UIView -> Loop through with Animation
问题描述
我有10个 UILabels
,也许有些的UIImageViews
。我想顺利淡出和放大器,显示在1 的UIView
这一切;淡入过渡,一前一后。
I have 10 UILabels
, maybe some UIImageViews
. I want to display all these in 1 UIView
with smooth FadeOut & FadeIn transition, one after the other.
我想想,把的UIView
动画在for循环将无法正常工作,动画完成异步和放大器;不会放弃应有的功效。所以,通常的方式我会做,这是链的UIView
动画在一起。即后的一个元素的动画后,才能开始下一个。 3-4元素code看起来确定。像这样 -
I know putting UIView
animations in a for-loop will not work as animations are done asynchronous & will not give proper effect. So the way normally I would do this is to chain UIView
animation together. i.e. after one elements animation is complete begin the next one. For 3-4 elements the code looks ok. Like so -
[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){ ... }
}
但对于10+的元素它变得非常混乱。如何将一个去这样做呢?基本上我创建一个的UIView
与循环内容,很像一个小部件。
But for 10+ elements it gets very messy. How would one go about doing this? Essentially I am creating a UIView
with looping content, much like a widget.
推荐答案
与的NSTimer
编辑,而不是循环。
计数
是在标题中定义的伊娃。
counter
is an ivar defined in the header.
- (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;
}
}
这篇关于iOS的10 UILabels - > 1 UIView的 - >通过与动画循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!