暂停UIImageview动画

时间:2012-06-19 14:32:54

标签: iphone uiimageview

我想暂停UIImageView animation并根据我的研究发现你可以停止图像的动画,但你不能暂停序列。通过在stop animating上调用语句UIImageView,它会停止动画并使图像视图空白。

要暂停UIImages的动画,必须使用NSTimer

我已经在app中使用一个NSTimer以不同的时间间隔依次加载view controllers。我在每个UIImages中都有view controller的幻灯片放映。

问题是当计时器暂停时view controllers暂停加载,但当时显示的view controller仍显示UIImages的{​​{1}}幻灯片重复,直到暂停恢复。所以我也想暂停那个幻灯片。

这是我使用view controller

的地方
NSTimer

这就是displayviewsAction以不同的时间间隔一个接一个地加载11个视图控制器的方法

-(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;
    }         
    } 
    }

这就是在视图控制器中开始图像动画的方法

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

现在,如果此人// 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]; 暂停,有人可以指导我如何暂停此UIImage animation幻灯片显示。

感谢您的帮助。

1 个答案:

答案 0 :(得分:9)

您可以使用apple提供的代码暂停和恢复动画。

-(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;
}

要让CALayer传递给pauseLayer函数,您可以使用以下代码。

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

希望它对你有所帮助。