如何重新开始CALayer动画?

时间:2015-03-06 08:37:22

标签: ios macos cocoa cocoa-touch calayer

Apple提到暂停和恢复CALayer动画的这两种方法

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

但如何重新启动动画?

这个其他的SO问题与我需要的东西无关。

1 个答案:

答案 0 :(得分:1)

我在ViewController中采用了来自blog的代码。看一下方法 [resetEmitterLayer:] 。在您的情况下,重置动画所需要做的就是:

  1. 从超级图层中删除发射器层。
  2. 重新创建发射器层。
  3. 将新的发射器图层添加到图层树。
  4. 请参阅下面的ViewController实现

    #import "EmitterViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @interface EmitterViewController ()
    
    @property (nonatomic, strong) CAEmitterLayer *emitterLayer;
    
    @end
    
    @implementation EmitterViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        self.emitterLayer = [self createEmitterLayer];
    
        [self.view.layer addSublayer:self.emitterLayer];
    }
    
    -(CAEmitterLayer *)createEmitterLayer {
    
        CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
        emitterLayer.emitterPosition = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.origin.y);
        emitterLayer.emitterZPosition = 10;
        emitterLayer.emitterSize = CGSizeMake(self.view.bounds.size.width, 0);
        emitterLayer.emitterShape = kCAEmitterLayerSphere;
    
    
        CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
        emitterCell.scale = 0.1;
        emitterCell.scaleRange = 0.2;
        emitterCell.emissionRange = (CGFloat)M_PI_2;
        emitterCell.lifetime = 5.0;
        emitterCell.birthRate = 10;
        emitterCell.velocity = 200;
        emitterCell.velocityRange = 50;
        emitterCell.yAcceleration = 250;
    
        emitterCell.contents = (id)[[UIImage imageNamed:@"WaterDrop.png"] CGImage];
    
        emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];
    
        return emitterLayer;
    }
    
    
    -(IBAction)pauseEmitterLayer:(id)sender
    {
        CFTimeInterval pausedTime = [self.emitterLayer convertTime:CACurrentMediaTime() fromLayer:nil];
        self.emitterLayer.speed = 0.0;
        self.emitterLayer.timeOffset = pausedTime;
    }
    
    -(IBAction)resumeEmitterLayer:(id)sender
    {
        CFTimeInterval pausedTime = [self.emitterLayer timeOffset];
        self.emitterLayer.speed = 1.0;
        self.emitterLayer.timeOffset = 0.0;
        self.emitterLayer.beginTime = 0.0;
        CFTimeInterval timeSincePause = [self.emitterLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
        self.emitterLayer.beginTime = timeSincePause;
    }
    
    -(IBAction)resetEmitterLayer:(id)sender
    {
        [self.emitterLayer removeFromSuperlayer];
    
        self.emitterLayer = [self createEmitterLayer];
    
        [self.view.layer addSublayer:self.emitterLayer];
    }
    
    @end