我第一次尝试使用 NSInvocation,下面的代码是从 stackoverflow 的其他答案代码中采用的。
计时器运行良好,但是当它实际到期并在 (animationEnd:) 处执行代码时会崩溃
UIImageView* animationView = [animationViewArray objectAtIndex: i];
[self.imageView addSubview: animationView];
[animationView startAnimating];
// [NSTimer scheduledTimerWithTimeInterval: 5.5 target: self selector: @selector(animationEnd:) userInfo: animationView repeats: NO];
SEL selector = @selector(animationEnd:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
// when passing to timer, it's ok
// [animationView retain];
//Set the arguments
[invocation setTarget:self];
[invocation setArgument:&animationView atIndex:2];
[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:invocation repeats:NO];
-(void) animationEnd:(NSInvocation*) invocation
{
UIImageView* animationView = nil;
[invocation getArgument:&animationView atIndex:2];
[animationView removeFromSuperview];
[animationView release];
}
我哪里搞砸了?
根据崩溃日志,看起来 (animationEnd:) 处的调用是我传递给调用的参数本身。
混乱的东西。。
谢谢你。