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

Dav*_*nte 4 xcode uilabel ios5

我有一个显示图像的iOS5应用程序.我点击图片显示其信息.我希望这些信息在几秒钟后消失.有一个很好的方法来做到这一点?

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

谢谢!

Wri*_*sCS 19

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

例:

的NSTimer

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeOutLabels:) userInfo:nil repeats:NO];
Run Code Online (Sandbox Code Playgroud)

performSelector:withObject:afterDelay:

/* starts the animation after 3 seconds */
[self performSelector:@selector(fadeOutLabels) withObject:nil afterDelay:3.0f];
Run Code Online (Sandbox Code Playgroud)

你将调用方法(或任何你想要的方法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];
                     }];
}
Run Code Online (Sandbox Code Playgroud)

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

-(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];
                     }];
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您收到枚举类型'enum UIViewAnimationCurve'的警告,请将其更改为'UIViewAnimationOptionCurveEaseInOut' (3认同)