I have an NSArray of strings that I would like to animate with a simple fade in/out animation.
----------------------------------------------------------------------------------
| fade in
| String 1 ---> displayed in UILabel
| fade out
----------------------------------------------------------------------------------
followed by
----------------------------------------------------------------------------------
| fade in
| String 2
| fade out
----------------------------------------------------------------------------------
etc…
Each one holding it’s position for about 5 seconds before it continues to the next item in the array. I’m not sure where to start… I know how to perform animations but I’m not quite sure how to animate through an array of strings with a 5 second (non-thread-blocking) pause on each iteration. Any ideas?
UPDATE MY SOLUTION BASED ON ACCEPTED ANSWER
- (void)startTimer {
arrayIndex = 0;
NSArray *myarray = [alerts_dict allValues];
breakingLabel.text = [myarray objectAtIndex:frameCount];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1];
breakingLabel.alpha = 1;
[UIView commitAnimations];
arrayIndex++;
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}
- (void)myFunction {
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
breakingLabel.alpha = 0;
}
completion:^(BOOL finished) {
NSArray *myarray = [alerts_dict allValues];
if (arrayIndex == [myarray count])
arrayIndex = 0;
breakingLabel.text = [myarray objectAtIndex:arrayIndex];
arrayIndex++;
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
breakingLabel.alpha = 1;
} completion:nil];
}];
}
You can do this pretty easily using block animations:
alphadown to 0.0textto the new string and animate it’salphaback to 1.0 (or original value, whatever it was)This routine can be invoked repeatedly by an
NSTimertimeout callback. Just instantiate a timer with interval 5.0, specify the callback and schedule it to run in the run loop like this: