Нажав кнопку, чтобы пройти каждый кадр анимации - PullRequest
0 голосов
/ 24 января 2012

Я пытаюсь создать приложение, в котором при нажатии кнопки оно переходит к следующему кадру в анимации.

У меня есть 8 файлов изображений, и когда я нажимаю кнопку, я хочу, чтобы 1-е изображение отображалось, и когда я нажимаю кнопку еще раз, я хочу, чтобы 2-е изображение заменяло 1-е изображение и т. Д.

Я думал что-то вроде:

-(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"];

}

и как-то заставлял все это работать последовательно с каждым нажатием.

Я довольно новичок в цели c, поэтому любая помощь будет очень полезнаоценили.

Может кто-нибудь подбросить пример кода для этого?

1 Ответ

1 голос
/ 24 января 2012

Давайте подумаем об этом.Если вы хотите сделать что-то последовательно, это похоже на работу массива.Итак, что вы думаете об этом:

В вашем .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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...