Objective c 在视图上显示图像1秒

Objective c 在视图上显示图像1秒,objective-c,ios,uiimageview,nstimer,Objective C,Ios,Uiimageview,Nstimer,我想了解的是: 当我按下按钮时,图像在视图上显示1秒,然后图像消失 我知道NSTimer会有帮助,但我不知道如何编写正确的代码…需要您的帮助,谢谢 - (IBAction)bodytouched:(id)sender { bodytouchedimage.hidden = NO; bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beated.jpg"]]; bodytouc

我想了解的是:

当我按下按钮时,图像在视图上显示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;
}

与使用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类的一个方法。

与其使用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类的一个方法。

当您触摸按钮时,您应该调用
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;
}

您应该在触摸按钮时调用
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;
}

有什么不管用的?看起来应该能用。这不是给你的吗?现在,它起作用了。我在.xib文件的视图上放了一个UIImageView,建立连接,为它选择一个图像,然后删除第3行的代码。这有什么不起作用?看起来应该能用。这不是给你的吗?现在,它起作用了。我在.xib文件的视图上放了一个UIImageView,建立连接,为它选择了一个图像,并删除了第3行的代码。它可以工作!谢谢!顺便说一句,你错过了@selector(hidePicture:)之后的“:”它可以工作!谢谢!顺便说一句,你错过了@selector(hidePicture:)之后的“:”