xCode ios5:如何在间隔后淡出标签文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10097068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:56:25  来源:igfitidea点击:

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

xcodeios5uilabel

提问by David DelMonte

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?

我有一个显示图像的小 iOS5 应用程序。我单击图像以显示其信息。我希望信息在几秒钟后消失。有没有好的方法可以做到这一点?

I can always implement another button action but this would be neater..

我总是可以实现另一个按钮操作,但这会更整洁..

Thanks!

谢谢!

回答by WrightsCS

Use either NSTimeror performSelector:withObject:afterDelay. Both methods require you to call a separate method that will actually do the fading out which should be fairly straightforward.

使用NSTimerperformSelector:withObject:afterDelay。这两种方法都要求您调用一个单独的方法,该方法实际上会进行淡出,这应该相当简单。

Example:

例子:

NSTimer

定时器

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeOutLabels:) userInfo:nil repeats:NO];

performSelector:withObject:afterDelay:

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)

您将调用该方法(或任何您想调用的方法fadeOutLabels

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