你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios animation的暫停pause、恢復resume

ios animation的暫停pause、恢復resume

編輯:IOS開發綜合
項目中要用到動畫的控制,比如說暫停、恢復繼續等,先看代碼:(很容易實現)
-(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;
}

如果在一個view中我們添加了多個動畫,(不是組合動畫),同樣容易實現動畫的暫停、恢復,方法就是為每個動畫層layer調用上面的方法,比如我的方法如下,我用的是三個單獨動畫的組合效果,就是如下調用的


-(void)stopAnimation;
{
    NSLog(@"停止動畫");
    //[_customView.layer removeAllAnimations];
    [self pauseLayer:_customView1.layer];
    [self pauseLayer:_customView2.layer];
    [self pauseLayer:_customView3.layer];
}

-(void)resumeAnimation
{
    [self resumeLayer:_customView1.layer];
    [self resumeLayer:_customView2.layer];
    [self resumeLayer:_customView3.layer];

}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved