Call method with parameters after animation completion
I want to call a method with certain parameters once an animation is done.
The flow is something like this:
-(void) myMethod:(int)val
{
[self performAnimation];
[self doSomethingElse:val]; // This should be done after animation completion
}
I presume the 'doSomethingElse' method needs to be called from the method defined in 'setAnimationDidStopSelector' - or is there a way to have the animation block until done?
What is the best way to let the method called on 'setAnimationDidStopSelector' know about the method it needs to call and its parameter? Can this be done with selectors? Or is the 开发者_StackOverflow社区only way of doing this by storing the methods and its params in class temp variables and access them when needed?
The performAnimation will not block the trhread so the only way to know if the animation is finished is to set the selector like this:
-(void) myMethod:(int)val {
[self setAnimationDelegate:self];
[self setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[self performAnimation];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
}
The method that's called via setAnimationDidStopSelector
has this signature:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
The context
parameter contains whatever you pass to beginAnimations:context:
so you could use it to pass val
to animationDidStop:finished:context:
. If the actual method you need to call is also a variable, you should create an NSInvocation
and pass it as the context.
There is no way to block the animation until it's done (unless you just block the main thread for the animation duration. You can pass two arguments to the didStopSelector method: the animation name and context. Name has to be a string, but context can be anything.
精彩评论