Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xCode ios5: How to fade out a label text after an interval?

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!

like image 485
ICL1901 Avatar asked Dec 01 '22 00:12

ICL1901


1 Answers

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.

Example:

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];
                     }];
}
like image 151
WrightsCS Avatar answered Dec 04 '22 06:12

WrightsCS