Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause UIImageview Animation

I want to pause UIImageView animation and based on my research found that you can stop the animation of the images but you cannot pause the sequence. By calling statement stop animating on the UIImageView then it stops the animation and blanks the image view.

To pause the animation of UIImages one has to use NSTimer.

I m already using one NSTimer in app for loading view controllers one after the other at different time intervals. I have a slide show of UIImages in each view controller.

Issue is when Timer is paused view controllers are paused from loading further but the view controller which is on display at that time still shows the slide show of UIImages of that view controller repeatedly until pause is resumed. So I want to pause that slideshow too.

This is where i m using the NSTimer

-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
    [sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
    [audioPlayer pause];
    [self pauseTimer];
}else{
    [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    [audioPlayer play];
    [self resumeTimer];
if(isFirstTime == YES)
    {
     self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                         target:self
                                         selector:@selector(displayviewsAction:)
                                         userInfo:nil
                                        repeats:NO];
        isFirstTime  = NO;
    }         
    } 
    }

This is how displayviewsAction loads 11 view controllers one after another at different time intervals

- (void)displayviewsAction:(id)sender
 {  
 First *firstController = [[First alloc] init];
 firstController.view.frame = CGRectMake(0, 0, 320, 480);
 CATransition *transitionAnimation = [CATransition animation];
 [transitionAnimation setDuration:1];
 [transitionAnimation setType:kCATransitionFade];
 [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
 [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
 [self.view addSubview:firstController.view];
 [self.view addSubview:toolbar];
 [firstController release];   
 self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(Second) userInfo:nil repeats:NO];     

 }

 -(void)Second 
{
Second *secondController = [[Second alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionReveal];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionReveal];
[self.view addSubview:secondController.view]; 
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:@selector(Third) userInfo:nil repeats:NO];
}

And that is how starting image animation within the view controller

// Displays UIImageView
UIImageView* ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 300, 235)];
self.view.backgroundColor = [UIColor brownColor];
// load all the frames of our animation
ImageView.animationImages = [NSArray arrayWithObjects:    
                                [UIImage imageNamed:@"1a.png"],
                                [UIImage imageNamed:@"1b.png"],
                                nil];

 // all frames will execute in 25 seconds
 ImageView.animationDuration = 25;
// start animating
[ImageView startAnimating];
ImageView.layer.borderWidth = 2;
ImageView.layer.borderColor = [[UIColor whiteColor]CGColor];
[ImageView.layer setMasksToBounds:YES];
[ImageView.layer setCornerRadius:15.0f];
[baseView addSubview:ImageView]; 

Now if someone can guide me how to pause this slideshow of UIImage animation when this NSTimer pauses.

Thanks for help.

like image 613
user1452248 Avatar asked Dec 26 '22 22:12

user1452248


1 Answers

you can use the code provided by apple to pause and resume your animation.

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

And to get the CALayer to pass to the pauseLayer function, you can use the following code.

CALayer *player = ImageView.layer;
[self pauseLayer:player];

Hope it helps you.

like image 189
user1120133 Avatar answered Jan 15 '23 19:01

user1120133