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

a 夏天 提交于 2019-12-21 05:30:54

问题


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!


回答1:


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];
                     }];
}


来源:https://stackoverflow.com/questions/10097068/xcode-ios5-how-to-fade-out-a-label-text-after-an-interval

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!