Reputation: 779
I'm using NSTimer (5 second duration) with selector to select randomly name from NSArray.
Here is some code
....
NSDate *endtime = [NSDate dateWithTimeIntervalSinceNow:5];
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(selectPlayer:)
userInfo:endtime
repeats:YES ];
...
-(void)selectPlayer:(NSTimer *)timer
{
if ([timer.userInfo timeIntervalSinceNow] < 0)
{
[timer invalidate];
}
NSString *playerName = [[NSString alloc]initWithFormat:@"%@",
[self.playersNames objectAtIndex:random() & [self.playersNames count]-1]];
self.playersLabel.text = playerName;
[playerName release];
}
That code works pefectly. self.playersLabel is populated with random name every 0.2 seconds.
What I want is to add some fadein/fadeout animation effect while the names are changed in playersLabel.
How to achieve that ?
Upvotes: 3
Views: 1898
Reputation: 1867
You just have to add an animation when you change the text of playersLabel
Here is how you can achieve this :
[UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
animations:^{
playersLabel.alpha = 0.f;
} completion:^(BOOL complete){
[UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
animations:^{
playersLabel.text = playerName;
playersLabel.alpha = 1.f;
} completion:NULL];
}];
I used the completion block of the first animation to reset back the alpha value of the label. Each animation lasts 0.1 because your playerName
is chosen each 0.2 seconds (0.1*2).You could have used the UIViewAnimationOptionAutoreverse
, but if I remember it does not reset the alpha property of the label.
Upvotes: 1
Reputation: 100761
you can use this for setting the text:
[UIView animateWithDuration:0.4 animations:^{
self.playersLabel.alpha = 0;
} completion:^(BOOL finished) {
self.playersLabel.text = @"Other text";
[UIView animateWithDuration:0.4 animations:^{
self.playersLabel.alpha = 1;
}];
}];
Upvotes: 8