问题
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.
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){ ... }
}
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.
回答1:
Edited with NSTimer
instead of a loop.
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;
}
}
来源:https://stackoverflow.com/questions/7840261/ios-10-uilabels-1-uiview-loop-through-with-animation