NSTimer改变形象iPhone编程

如何在iPhone编程中使用NSTimer定期更改图像?

我创build一个图像视图来加载图像。

我想在imageview中显示图像,并通过使用NSTimer定期更改图像。

将您喜欢的图像导入到您的捆绑应用程序中,称为image1.png,image2.png image3.png等等。

在界面构build器中添加UIImageview调用它(引用)“theImage”

在H文件中:

@interface delme3ViewController : UIViewController { UIImageView *theImage; int imageCount; } @property (nonatomic, retain) IBOutlet UIImageView *theImage; @end 

在M文件中:

 - (void)viewDidLoad { [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(animateFunction) userInfo:nil repeats:YES]; imageCount = 1; } -(void)animateFunction { NSLog(@"Timer heartbeat %i",imageCount); imageCount = imageCount + 1; NSString *imageName = [NSString stringWithFormat:@"image%i.png"]; [theImage setImage:[UIImage imageNamed:imageName]]; } 

而不是使用NSTimer,我只是使用UIImages文件和animation。 看到这里的文档。

您还可以使用图像名称数组,而不是重命名包中的图像。 🙂