在视图上显示图像1秒

时间:2012-03-12 20:39:47

标签: objective-c ios uiimageview nstimer

我想要实现的是:

当我触摸按钮时,图像在视图上显示1秒钟,然后图像消失。

我知道NSTimer会有所帮助,但我不知道如何编写正确的代码......需要你的帮助,谢谢。

- (IBAction)bodytouched:(id)sender {  
bodytouchedimage.hidden = NO;
    bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beated.jpg"]];
    bodytouchedimage.userInteractionEnabled = YES;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showPictures:) userInfo:nil repeats:NO];
}


- (void)showPictures:(NSTimer *)timer {     
bodytouchedimage.hidden = YES;
}

2 个答案:

答案 0 :(得分:3)

当您触摸按钮时,您应该调用showPictures函数,然后在showPictures方法中添加NSTimer,这将在1秒后调用hidePictures方法

- (void)showPictures 
{
    bodytouchedimage.hidden = NO;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hidePictures) userInfo:nil repeats:NO];
}

- (void)hidePictures
{
    bodytouchedimage.hidden = YES;
}

答案 1 :(得分:1)

不是使用NSTimer,而是简单地调用方法隐藏图像会更容易:

- (IBAction)bodytouched:(id)sender {  
bodytouchedimage.hidden = NO;
    bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beated.jpg"]];
    bodytouchedimage.userInteractionEnabled = YES;
    [self performSelector:@selector(hidePicture) withObject:nil afterDelay:1];
}


- (void)hidePicture {     
bodytouchedimage.hidden = YES;
}

performSelector:withObject:afterDelay:是NSObject类的一个方法。