【发布时间】:2011-12-02 22:51:22
【问题描述】:
如何在 iPhone 编程中使用 NSTimer 定期更改图像?
我创建了一个用于加载图像的图像视图。
我想在 imageview 中显示图像并使用 NSTimer 定期更改图像。
【问题讨论】:
如何在 iPhone 编程中使用 NSTimer 定期更改图像?
我创建了一个用于加载图像的图像视图。
我想在 imageview 中显示图像并使用 NSTimer 定期更改图像。
【问题讨论】:
我不会使用 NSTimer,而是使用一组 UIImage 并为其设置动画。请参阅文档from here。
【讨论】:
将您喜欢的图像导入您的捆绑应用程序 称它们为 image1.png、image2.png image3.png 等等....
在界面生成器中添加 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]];
}
【讨论】:
您还可以使用图像名称数组,而不是重命名捆绑包中的图像。 :)
【讨论】: