【问题标题】:xCode ios5: How to fade out a label text after an interval?xCode ios5:如何在间隔后淡出标签文本?
【发布时间】:2012-04-10 21:57:42
【问题描述】:

我有一个显示图像的 iOS5 小应用程序。我单击图像以显示其信息。我希望信息在几秒钟后消失。有什么好的方法吗?

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

谢谢!

【问题讨论】:

    标签: xcode ios5 uilabel


    【解决方案1】:

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

    示例:

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

    然后你会调用方法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];
                         }];
    }
    

    或者您可以使用动画块来完成所有工作:

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

    【讨论】:

    • 这很好用,只有一个问题。它适用于第一张图片。但是标签不会随后续图像恢复。很抱歉很密集..它的寿命很长:​​)
    • 我想我应该为其他人添加 - 我使用了动画块方法,取出了 removeFromSuperview 语句,并在点击图像 id 之前将 myLabel.alpha 设置回 1.0..
    • 同意,很好的答案。代码块...必须爱他们。
    • 如果您收到枚举类型 'enum UIViewAnimationCurve' 的警告,请将其更改为 'UIViewAnimationOptionCurveEaseInOut'
    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多