核心动画 (Core Animation)

2017-01-23  本文已影响0人  CoderZNB

CALayer上动画的暂停和恢复

OC语法

#pragma mark 暂停CALayer的动画
-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

    // 让CALayer的时间停止走动
      layer.speed = 0.0;
    // 让CALayer的时间停留在pausedTime这个时刻
    layer.timeOffset = pausedTime;
}

#pragma mark 恢复CALayer的动画
-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = layer.timeOffset;
    // 1. 让CALayer的时间继续行走
      layer.speed = 1.0;
    // 2. 取消上次记录的停留时刻
      layer.timeOffset = 0.0;
    // 3. 取消上次设置的时间
      layer.beginTime = 0.0;
    // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
      layer.beginTime = timeSincePause;
}

swift语法

import UIKit

extension CALayer {
    func pauseAnimation() {
        let pauseTime = convertTime(CACurrentMediaTime(), fromLayer: nil)
        speed = 0.0
        timeOffset = pauseTime
    }

    func resumeAnimation() {
        // 1.取出时间
        let pauseTime = timeOffset

        // 2.设置动画的属性
        speed = 1.0
        timeOffset = 0.0
        beginTime = 0.0

        // 3.设置开始动画
        let startTime = convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        beginTime = startTime
    }
}

CAKeyframeAnimation 的简单使用

示例代码

- (NSArray *)fishArray {

    if (_fishArray == nil) {

        //加载图片
        NSMutableArray *tempArray = [NSMutableArray array];
        for (int i= 0; i < 10; i++) {
            NSString *name = [NSString stringWithFormat:@"fish%d",i];
            UIImage *image = [UIImage imageNamed:name];
            [tempArray addObject:image];
        }
        _fishArray = tempArray;
    }
    return _fishArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建背景图片
    self.view.layer.contents = (id)[UIImage imageNamed:@"bg"].CGImage;

    //创建一个layer
    CALayer *fishL = [CALayer layer];
    fishL.frame = CGRectMake(100, 288, 89, 40);
    fishL.contents = (id)[UIImage imageNamed:@"fish0"].CGImage;
    [self.view.layer addSublayer:fishL];
    self.fishL = fishL;

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(change) userInfo:nil repeats:YES];



}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //添加动画
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(100, 288)];
    [path addLineToPoint:CGPointMake(100, 100)];
    [path addQuadCurveToPoint:CGPointMake(300, 400) controlPoint:CGPointMake(300, 20)];
    [path addLineToPoint:CGPointMake(100, 288)];
    //根据一个路径做动画
    anim.path = path.CGPath;
    //设置旋转模式
//    anim.rotationMode = @"autoReverse";
    anim.rotationMode = kCAAnimationRotateAutoReverse;
    //设置时间模型
    anim.calculationMode = @"cubicPaced";

    anim.repeatCount = HUGE;

    anim.duration = 5;

    //添加动画
    [self.fishL addAnimation:anim forKey:@"fish"];


}

static int _fishIndex = 0;
- (void)change {
    _fishIndex++;
    if (_fishIndex == 10) {
        _fishIndex = 0;
    }
    UIImage *image = self.fishArray[_fishIndex];
    self.fishL.contents = (id)image.CGImage;
}
上一篇 下一篇

猜你喜欢

热点阅读