我将一组图像加载到一个uiimageview中,并在一个周期内设置动画。显示图像后,我希望调用@selector,以便关闭当前视图控制器。使用以下代码可以很好地设置图像的动画:

NSArray * imageArray = [[NSArray alloc] initWithObjects:
                        [UIImage imageNamed:@"HowTo1.png"],
                        [UIImage imageNamed:@"HowTo2.png"],
                        nil];
UIImageView * instructions = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

instructions.animationImages = imageArray;
[imageArray release];
instructions.animationDuration = 16.0;
instructions.animationRepeatCount = 1;
instructions.contentMode = UIViewContentModeBottomLeft;
[instructions startAnimating];
[self.view addSubview:instructions];
[instructions release];

16秒后,我想调用一个方法。我查看了UIView类方法setAnimationDidStopSelector:,但无法使其与当前的动画实现协同工作。有什么建议吗?
谢谢。

最佳答案

使用performSelector:withObject:afterDelay:

[self performSelector:@selector(animationDidFinish:) withObject:nil
    afterDelay:instructions.animationDuration];

或使用dispatch_after
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self animationDidFinish];
});

08-18 15:14