xCode ios5:如何在间隔后淡出标签文本? [英] xCode ios5: How to fade out a label text after an interval?
问题描述
我有一个显示图像的iOS5小应用程序。我点击图片显示其信息。我希望这些信息在几秒钟后消失。有没有一个很好的方法来做到这一点?
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..
谢谢!
推荐答案
使用 NSTimer
或 performSelector:withObject:afterDelay
。这两种方法都要求你调用一个实际上会逐渐淡出的单独方法,这个方法应该相当简单。
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.
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];
}];
}
或者您可以使用动画块来完成所有工作:
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];
}];
}
这篇关于xCode ios5:如何在间隔后淡出标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!