views:

44

answers:

3

I have the following code but doesnt work

[self performSelector:@selector([A1 setImage:[UIImage imageNamed:@"m1.10002.png"] forState:UIControlStateNormal])
                       withObject:A1 afterDelay:0.1];

How can I execute the following statement after a certain time

[A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"m1.a0009.png"]] forState:UIControlStateNormal];

I would like to set a series of 5 images to execute a small animation effect. but am not sure how it is done.

Thanks

+3  A: 

Have a method like this in your code:

- (void) blahBlahWhatever {
    [A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"m1.a0009.png"]] forState:UIControlStateNormal];
}

and then use

[self performSelector:@selector(blahBlahWhatever) afterDelay:0.1];

to call it.


If you want to call the method with a custom NSString, just allow an NSString input:

-(void) blahBlahWhatever:(NSString *)string {
    [A1 setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
}

and then use

NSString *fileString = @"m1.a0009.png"; // or whatever this might be
[self performSelector:@selector(blahBlahWhatever:) withObject:fileString afterDelay:0.1];

to call it from your code.

esqew
Thanks, and If I would like to set the string differently depending on where it is being called?
alJaree
Oh, that's easy. I'll give you an updated answer in just a second.
esqew
I've added the other solution.
esqew
Last line of code is still wrong . . . it is using the code provided by the OP instead of the `@selector(blahBlahWhatever:)` that you provided earlier.
dreamlax
@ seanny94 Thanks a LOT :P
alJaree
@seanny94 I fixed @dreamlax's comment for you (I'm assuming you just spaced it out). :p
Wevah
Oh god... .-. I dunno how I missed that. Thanks!
esqew
Another question would be what If I wanted to set the e.g A1 also depending where it was called. A1 setImage:
alJaree
A: 

You should wrap your snip code in a method, and then call this method at @Selector

Son Nguyen
+1  A: 

I think you want to look at UIView's animateWithDuration:animations:completion:

(I'm assuming A1 is a button or something like that)

Something like

animate(UIButton b, int state, int last_state) {
  if (state < last_state) {
    float duration = 1.0 / 30.0;
    [b animateWithDuration:1.0/30
      animations: ^{ [UIImage imageNamed:[NSString stringWithFormat:@"m%d.a0009.png", state]]; }
      completion: ^{ animate(b, state++, last_state); }];
  }
}

To animate images from m0 to m4 call animate(A1, 0, 5). You'll likely want a better set of arguments and the like (also I chose 30FPS, you might like 10 or 20...or to make that an argument).

Stripes