gpt4 book ai didi

iphone - 点击按钮浏览动画的每一帧

转载 作者:行者123 更新时间:2023-11-29 04:50:04 26 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,当您点击按钮时,它将转到动画中的下一帧。

我有 8 个图像文件,当我按下按钮时,我希望显示第一张图像,当我再次按下按钮时,我希望第二张图像替换第一张图像,依此类推。

我在想这样的事情:

-(IBAction)buttonPressDoStuff:(id)sender {
imageThing.image = [UIImage imageNamed:@"image1.png"];
imageThing.image = [UIImage imageNamed:@"image2.png"];
imageThing.image = [UIImage imageNamed:@"image3.png"];
imageThing.image = [UIImage imageNamed:@"image4.png"];

}

并以某种方式使这一切在每次按下时连续工作。

我对 objective-c 相当陌生,因此我们将不胜感激。

任何人都可以提供一些示例代码来执行此操作吗?

最佳答案

让我们考虑一下这个问题。如果你想按顺序做某事,这听起来像是数组的工作。那么您对此有何看法:

在您的 .h 文件中,添加以下实例变量:

NSMutableArray* picturesArray;
NSInteger counter;

现在在您的 .m 文件中,在您的类的 init 方法中:

//this loop will fill your array with the pictures
for(int idx = 0; idx < NUMBER_OF_PICTURES; idx++) {
//IMPORTANT: this assumes that your pictures' names start with
//'image0.png` for the first image, then 'image1.png`, and so on

//if your images' names start with 'image1.png' and then go up, then you
//should change the 'int idx = 0' declaration in the for loop to 'int idx = 1'
//so the loop will start at 0. You will then need to change the condition
//to 'idx < (NUMBER_OF_PICTURES + 1)' to accomodate the last image
NSString* temp = [NSString stringWithFormat:@"image%i.png", idx];
UIImage* tempImage = [UIImage imageNamed:temp];
[picturesArray addObject:tempImage];
}

并在您的 buttonPressDoStuff: 方法中:

//this method will move to the next picture in the array each time it is pressed
-(IBAction)buttonPressDoStuff:(id)sender {
if(counter < [pictureArray count]) {
imageThing.image = [picturesArray objectAtIndex:counter];
counter++;
}
}

您的 init 方法应如下所示:

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
//do setup here
for(int idx = 0; idx < NUMBER_OF_PICTURES; idx++) {
NSString* temp = [NSString stringWithFormat:@"image%i.png", idx];
UIImage* tempImage = [UIImage imageNamed:temp];
[picturesArray addObject:tempImage];
}
}
//it is important that you return 'self' no matter what- if you don't,
//you will get the 'control reached end of non-void method' warning
return self;
}

关于iphone - 点击按钮浏览动画的每一帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8981822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com