delaying statement execution objective c
I have the following code but doesnt work
[self performSelector:@selector([A1 setImage:[UIImage imageNamed:@"m1.10002.png"] forState:UIControlStateNormal])
withObject:A开发者_运维知识库1 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
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.
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).
You should wrap your snip code in a method, and then call this method at @Selector
精彩评论