IOS在视图控制器上制作一个对象在一段时间后消失了吗?

时间:2012-07-26 01:25:43

标签: ios

正如标题所说我希望我的一个物体在一段时间后消失。我在网上搜索了这个主题,我找不到任何答案或教程,所以我从这里问它。

示例:

用户打开应用程序。

视图控制器进入屏幕并且上面有标签。

该标签在5秒后消失。

我该怎么做?

我是否需要使用动画?

我真的更喜欢没有任何动画(如果制作动画很容易就可以了)

你能逐步解释一下吗我会先尝试让我的标签消失然后我会让一个视图对象消失。我知道想要一个视图消失的奇怪我可以使用另一个视图控制器但我不知道我想要它我想编码它以便在一段时间后消失。

感谢。

2 个答案:

答案 0 :(得分:8)

您所要做的就是设置一个计时器。例如,

//In your viewDidLoad method of your view controller
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(hideLabel) userInfo:nil repeats:NO];

//In a method called hideLabel
- (void) hideLabel
{
    self.myLabel.hidden = YES;  //This assumes that your label is a property of your view controller
}

这就是它的全部内容。

答案 1 :(得分:3)

启动这样的计时器:

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

然后当计时器点击开始动画时,这很容易:

- (void)hideWithAnimation {

    [UIView animateWithDuration:0.5
                          delay:0.0 
                        options:UIViewAnimationOptionTransitionCrossDissolve
                     animations:^{
                                     // Fade the label
                                     [myLabel setAlpha:0]
                                 };
                     completion:NULL];
}

以下是UIView查找Class方法的文档:

UIView class reference

相关问题