iOS Developer编程

Core Animation<2>

2017-03-15  本文已影响70人  牛奈奈

时间函数:动画总会在设定的时间长度内执行完,在这段时间内,动画的变化速度有多快,主要决定于时间函数

CA_EXTERN NSString * const kCAMediaTimingFunctionLinear  //线性函数
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn  //缓入
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut  //缓出
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut  //慢入慢出
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault  //默认
    CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png
系统提供的设置时间函数的方法[CAMediaTimingFunction functionWithControlPoints::::]里面的四个参数范围值都是0.0 -1.0之间,这些数值的由来可以看曲线图起始点和终点对应的切线。 基本动画.gif

思路:拆分动画

Paste_Image.png
   CAShapeLayer * first = [CAShapeLayer layer];
    first.path = [self wormRunLongPath];
    first.lineWidth = 10;
    first.lineCap = kCALineCapRound;
    first.lineJoin = kCALineJoinRound;
    first.strokeColor = [UIColor redColor].CGColor;
    first.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:first];

- (CGPathRef)wormRunLongPath {
    //中间点
    CGPoint center = CGPointMake(220, 300/2);
    CGFloat radius = 30;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(center.x  - radius , center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
   [path addArcWithCenter:CGPointMake(center.x  + radius, center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
    return path.CGPath;
}

运行,就得到了上面两个半圆的效果。

基本动画分解1.gif
   //第一阶段虫子的拉伸
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:0.5];
   //从0到0.5表示动画的过程从无到一个半圆
    animation.duration = 0.75;
    animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
    animation.removedOnCompletion = false;
    animation.fillMode = kCAFillModeForwards;

运行,OK.

基本动画分解2.gif
    //第一阶段虫子的缩小
    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animation1.fromValue = [NSNumber numberWithFloat:0.0];
    animation1.toValue = [NSNumber numberWithFloat:0.5];
    animation1.duration = 0.45;
    animation1.beginTime = 0.75;
    animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation1.fillMode = kCAFillModeForwards;

稍微注意一下我们就发现其实只是keyPath的区别,一个是start,一个是end,同事我们设置beginTime表示的是在2的效果完成之后才开始3的效果,刚好衔接上去就凑成了一个完整的动画。然后把这两个放到一个动画组里面试试效果.

基本动画分解3.gif
 CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray  arrayWithObjects:animation,animation1,nil];
    group.repeatCount = HUGE_VALF;
    group.duration = 0.75 * 2;
    [self.firstWormShapeLayer addAnimation:group forKey:@"one"];
基本动画分解4.gif 基本动画分解5.gif
//第二阶段虫子的拉伸
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation2.fromValue = [NSNumber numberWithFloat:0.5];
    animation2.toValue = [NSNumber numberWithFloat:1.0];
    animation2.duration = 0.75;
    animation2.beginTime = 1.2;
    animation2.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
    animation2.fillMode = kCAFillModeForwards;
    //第二阶段虫子的缩小
    CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animation3.fromValue = [NSNumber numberWithFloat:0.5];
    animation3.toValue = [NSNumber numberWithFloat:1];
    animation3.duration = 0.45;
    animation3.beginTime = 0.75 + 1.2;
    animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation3.fillMode = kCAFillModeForwards;
把位移加入到动画组里面运行就实现了红色的效果

![基本动画分解7.gif](https://img.haomeiwen.com/i1851080/6b2a75e429e3baf3.gif?imageMogr2/auto-orient/strip)

同理,可实现黄色和绿色的加入。

上一篇 下一篇

猜你喜欢

热点阅读