Question

I've searched everywhere but I did not find a proper solution for my problem. I have a image-sequence playing on start up. After this sequence I want to trigger a function. A function that activates an Image which. So the animation will run fluently into the image without break. I found a lot of solutions on the internet e.g. doing it with a timer but running the code via simulator it works, on device it doesn't and gets delayed :(. Is it a good way to set the timer just by testing when the animation is finished? The method "animationDone" is always triggered on the wrong time!

Here my example code:

- (void)viewDidLoad {

    imageView.animationImages = [NSArray arrayWithObjects:

                                 [UIImage imageNamed:@"Rad_start_00000.png"],
                                 [UIImage imageNamed:@"Rad_start_00001.png"],
                                 [UIImage imageNamed:@"Rad_start_00002.png"],
                                 [UIImage imageNamed:@"Rad_start_00003.png"],
                                 [UIImage imageNamed:@"Rad_start_00004.png"],
                                 [UIImage imageNamed:@"Rad_start_00005.png"],
                                 [UIImage imageNamed:@"Rad_start_00006.png"],
                                 [UIImage imageNamed:@"Rad_start_00007.png"],
                                 [UIImage imageNamed:@"Rad_start_00008.png"],
                                 [UIImage imageNamed:@"Rad_start_00009.png"],
                                 [UIImage imageNamed:@"Rad_start_00010.png"],
                                 [UIImage imageNamed:@"Rad_start_00011.png"],
                                 [UIImage imageNamed:@"Rad_start_00012.png"],
                                 [UIImage imageNamed:@"Rad_start_00013.png"],
                                 [UIImage imageNamed:@"Rad_start_00014.png"],
                                 [UIImage imageNamed:@"Rad_start_00015.png"],
                                 [UIImage imageNamed:@"Rad_start_00016.png"],
                                 [UIImage imageNamed:@"Rad_start_00017.png"],nil];

    [imageView setAnimationDuration:0.7];
    [imageView setAnimationRepeatCount:1];
    [imageView startAnimating];

    [self performSelector:@selector(animationDone) withObject:nil afterDelay:1.2];
}
Was it helpful?

Solution

First, you should try using the same interval for both the afterDelay parameter to performSelector and the setAnimationDuration (e.g., 0.7 seconds for both).

If that doesn't quite do what you want, you can programmatically call timer, constantly checking to see if the animation is done. For example, you could set up a display link (it's like a timer, but linked to updates to the display), and check imageView.isAnimating, e.g.:

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) CADisplayLink *displayLink;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imageView.animationImages = @[[UIImage imageNamed:@"Rad_start_00000.png"],
                                       [UIImage imageNamed:@"Rad_start_00001.png"],
                                       [UIImage imageNamed:@"Rad_start_00002.png"],
                                       [UIImage imageNamed:@"Rad_start_00003.png"],
                                       [UIImage imageNamed:@"Rad_start_00004.png"],
                                       [UIImage imageNamed:@"Rad_start_00005.png"],
                                       [UIImage imageNamed:@"Rad_start_00006.png"],
                                       [UIImage imageNamed:@"Rad_start_00007.png"],
                                       [UIImage imageNamed:@"Rad_start_00008.png"],
                                       [UIImage imageNamed:@"Rad_start_00009.png"],
                                       [UIImage imageNamed:@"Rad_start_00010.png"],
                                       [UIImage imageNamed:@"Rad_start_00011.png"],
                                       [UIImage imageNamed:@"Rad_start_00012.png"],
                                       [UIImage imageNamed:@"Rad_start_00013.png"],
                                       [UIImage imageNamed:@"Rad_start_00014.png"],
                                       [UIImage imageNamed:@"Rad_start_00015.png"],
                                       [UIImage imageNamed:@"Rad_start_00016.png"],
                                       [UIImage imageNamed:@"Rad_start_00017.png"]];

    [self.imageView setAnimationDuration:0.7];
    [self.imageView setAnimationRepeatCount:1];

    [self.imageView startAnimating];
    [self startDisplayLink];
}

- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
    if (!self.imageView.isAnimating) {
        [self stopDisplayLink];

        NSLog(@"done");

        // do whatever else you want here
    }
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top