Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Method After UIImageView Animation Finish

I have an array of images loaded into a UIImageView that I am animating through one cycle. After the images have been displayed, I would like a @selector to be called in order to dismiss the current view controller. The images are animated fine with this code:

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];

After the 16 seconds, I would like a method to be called. I looked into the UIView class method setAnimationDidStopSelector: but I cannot get it to work with my current animation implementation. Any suggestions?

Thanks.

like image 254
Kevin_TA Avatar asked Feb 14 '12 19:02

Kevin_TA


2 Answers

You can simple use this category UIImageView-AnimationCompletionBlock

And for Swift version: UIImageView-AnimationImagesCompletionBlock

Take a look on ReadMe file in this class..

Add UIImageView+AnimationCompletion.h and UIImageView+AnimationCompletion.m files in project and them import UIImageView+AnimationCompletion.h file where you want to use this..

For eg.

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for(int i = 10001 ; i<= 10010 ; i++)
    [imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];

self.animatedImageView.animationImages = imagesArray;
self.animatedImageView.animationDuration = 2.0f;
self.animatedImageView.animationRepeatCount = 3;
[self.animatedImageView startAnimatingWithCompletionBlock:^(BOOL success){
 NSLog(@"Animation Completed",);}];
like image 112
GurPreet_Singh Avatar answered Oct 13 '22 00:10

GurPreet_Singh


Use performSelector:withObject:afterDelay::

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

or use 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];
});
like image 36
rob mayoff Avatar answered Oct 13 '22 01:10

rob mayoff