I have a little iOS5 app that shows images. I click on the image to show its information. I'd like the information to fade away after a few seconds. Is there a good method to do this?
I can always implement another button action but this would be neater..
Thanks!
Use either NSTimer
or performSelector:withObject:afterDelay
. Both methods require you to call a separate method that will actually do the fading out which should be fairly straightforward.
NSTimer
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeOutLabels:) userInfo:nil repeats:NO];
performSelector:withObject:afterDelay:
/* starts the animation after 3 seconds */
[self performSelector:@selector(fadeOutLabels) withObject:nil afterDelay:3.0f];
And you will call the method fadeOutLabels
(or whatever you want to call it)
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:0.0 /* do not add a delay because we will use performSelector. */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}
Or you can use the animation block to do all of the work:
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:3.0 /* starts the animation after 3 seconds */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With