开发者

Changing image of UIImage with time

I want to change an UIImageView's image and 3 seconds later go back to the first image...This is my code:

-(void)change: (UIImageView *)sender
{
    UIImage *image = [UIImage imageNAmed: @"anImage"];
   开发者_JAVA百科 [sender setImage:image];
    [self performSelector: @selector(doNothing) withObject:nil afterDelay:3];
    [sender setImage: [UIImage imageNamed: @"firstImage"]];
}

It doesn't work.


Try:

-(void)change: (UIImageView *)sender
{
    UIImage *image = [UIImage imageNAmed: @"anImage"];
    [sender setImage:image];
    [sender performSelector: @selector(setImage:) withObject:[UIImage imageNamed: @"firstImage"] afterDelay:3];
}

performSelector schedules selector call with some delay. It does not delay the execution of current method


Could you post the code for doNothing too please?

If doNothing really does nothing, then what's happening is the doNothing method is being called after 3 seconds from when the following line executes:

[self performSelector: @selector(doNothing) withObject:nil afterDelay:3];

But here's the thing, the following line is being executed straight after your doNothing method has been scheduled to run after three seconds:

[sender setImage: [UIImage imageNamed: @"firstImage"]];

In other words, the method execution doesn't wait around for your doNothing method to complete, it just continues, and doNothing is run on a separate thread.


you have said to perform the function doNothing after 3 seconds of delay. So after 3 seconds it will get called so change the image back to firstImage in doNothing function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜