我试图逐帧更改UIImageView的图像,我有几个应该通过按钮更改的后续图像。这是我的代码:
- (IBAction)next:(id)sender {
for( i = 0; i <= 31; i++ ) {
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",i];
_image.image = [UIImage imageNamed:filename];
}
}
问题是我的代码将图像更改为仅第一帧,当我点按按钮
答案 0 :(得分:1)
因为你的变量值i总是0
尝试此解决方案:
在.h文件中创建实例变量
int imgCount;
在ViewDidLoad方法
中初始化它imgCount = 0;
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount]; // img0 is displayed
_image.image = [UIImage imageNamed:filename];
- (IBAction)next:(id)sender {
//first (by default) img0 is display
imgCount++;
if (imgCount > 30) { //suppose total image is 31 means 0 to 30
imgCount = 0;
}
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
_image.image = [UIImage imageNamed:filename];
}
- (IBAction)previous:(id)sender {
imgCount--;
if (imgCount < 0) { //suppose total image is 31 means 0 to 30
imgCount = 30;
}
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
_image.image = [UIImage imageNamed:filename];
}
答案 1 :(得分:0)
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"campFire01.gif"],
[UIImage imageNamed:@"campFire02.gif"],
[UIImage imageNamed:@"campFire03.gif"],
[UIImage imageNamed:@"campFire04.gif"],
[UIImage imageNamed:@"campFire05.gif"],
[UIImage imageNamed:@"campFire06.gif"],
[UIImage imageNamed:@"campFire07.gif"],
[UIImage imageNamed:@"campFire08.gif"],
[UIImage imageNamed:@"campFire09.gif"],
[UIImage imageNamed:@"campFire10.gif"],
[UIImage imageNamed:@"campFire11.gif"],
[UIImage imageNamed:@"campFire12.gif"],
[UIImage imageNamed:@"campFire13.gif"],
[UIImage imageNamed:@"campFire14.gif"],
[UIImage imageNamed:@"campFire15.gif"],
[UIImage imageNamed:@"campFire16.gif"],
[UIImage imageNamed:@"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
同时下载来源code form here.