动画IOS动画程序员

iOS之核心动画(全)

2016-03-16  本文已影响304人  李小南

核心动画(Core Animation)

Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。

CAAnimation

duration:动画的持续时间---(来自CAMediaTiming协议的属性)
repeatCount:重复次数,无限循环可以设置HUGE_VALF或者MAXFLOAT---(来自CAMediaTiming协议的属性)
repeatDuration:重复时间---(来自CAMediaTiming协议的属性)
removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards
fillMode:决定当前对象在非active时间段的行为。比如动画开始之前或者动画结束之后---(来自CAMediaTiming协议的属性)
beginTime:可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间---(来自CAMediaTiming协议的属性)
timingFunction:速度控制函数,控制动画运行的节奏
delegate:动画代理

CABasicAnimation——基本动画

      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //初始化动画对象
    CABasicAnimation *anim = [CABasicAnimation animation];
    
    //设置属性值
    anim.keyPath = @"transform.scale.x";
    anim.fromValue = @2;
    anim.toValue = @0.5;
    
    //动画完成时不要自动删除动画
    anim.removedOnCompletion = NO;
    anim.duration = 3;
    //保存动画最前面的效果.
    anim.fillMode = kCAFillModeForwards;
    //动画的重复次数
    anim.repeatCount = 2;
    
    //提交动画----第二个参数是标记动画的
    [self.redView.layer addAnimation:anim forKey:nil];
}

CAKeyframeAnimation——关键帧动画

//图标抖动
  - (void)iconAnim
{
    //帧动画
    CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animation];
    //设置动画属性为旋转
    keyAnim.keyPath = @"transform.rotation";
    //设置属性值为多个属性
    keyAnim.values = @[@angle2Radio(5), @angle2Radio(-5), @angle2Radio(5)];
    //设置动画执行次数
    keyAnim.repeatCount =  CGFLOAT_MAX;
    keyAnim.duration = 0.5;
    //    keyAnim.autoreverses = YES;
    
    [self.iconImageV.layer addAnimation:keyAnim forKey:nil];
}
  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animation];
    keyAnim.keyPath = @"position";  
    //创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(200, 50)];
    [path addLineToPoint:CGPointMake(200, 200)];
    //设置路径
    keyAnim.path = path.CGPath;
    keyAnim.duration = 2;
    
    [self.iconImageV.layer addAnimation:keyAnim forKey:nil];
}

CATransition——转场动画

Snip20160316_21.png
      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
      {
          //转场动画
          CATransition *anim = [CATransition animation];
    
          //设置转场类型
          anim.type = @"suckEffect";  //收缩效果,如一块布被抽走--多利用一个view制造假象
          anim.duration = 1;
    
          [self.imageV.layer addAnimation:anim forKey:nil];
    
          //转场代码      ------转场代码必须和转场动画在一起
          _i++;
          if (_i > 3) {
              _i = 1;
          }
    
          NSString *str = [NSString stringWithFormat:@"%d", _i];
          self.imageV.image = [UIImage imageNamed:str];
      }

CAAnimationGroup——动画组

   - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //动画组
    CAAnimationGroup *anim = [CAAnimationGroup animation];
    
    //移动
    CABasicAnimation *anim1 = [CABasicAnimation animation];
    
    anim1.keyPath = @"position.y";
    anim1.toValue = @400;
    anim1.duration = 2;
    
    //缩放
    CABasicAnimation *anim2 = [CABasicAnimation animation];
    
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @0.5;
    anim2.duration = 2;

    //自动执行数组中每一个动画对象
    anim.animations = @[anim1, anim2];
    anim.duration = 2;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    [self.greenView.layer addAnimation:anim forKey:nil];
}

CAGradientLayer--渐变层

可以设置渐变的方向:
通过startPoint和endPoint这两个属性来设置渐变的方向.它的取值范围也是(0~1)

默认方向为上下渐变为:
gradientL.startPoint = CGPointMake(0, 0);
gradientL.endPoint = CGPointMake(0, 1);
设置左右渐变
gradientL.startPoint = CGPointMake(0, 0);
gradientL.endPoint = CGPointMake(1, 0);

可以设置渐变从一个颜色到下一个颜色的位置
设置属性为locations = @[@0.3,@0.7]

渐变层同时还有一个opacity属性.这个属性是调协渐变层的不透明度.它的取值范围同样也是0-1,
当为0时代表透明, 当为1明,代码不透明.

所以我们可以给下部分图片添加一个渐变层,渐变层的颜色为从透明到黑色.
gradientL.colors =
@[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
    //创建渐变层
    CAGradientLayer *gradient = [CAGradientLayer layer];
    //设置渐变层的大小和图片frame一样大
    gradient.frame = self.buttomImageView.bounds;
    //设置渐变层的颜色
    gradient.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor];
    //设置渐变层的方向
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(0, 1);
 
    //设置渐变层的不透明度
    gradient.opacity = 0;
    //添加渐变层
    [self.buttomImageView.layer addSublayer:gradient];

CAReplicatorLayer--复制层

    //创建复制层
    CAReplicatorLayer *repL = [CAReplicatorLayer layer];
    repL.frame = self.contentViiew.bounds;
    [self.contentViiew.layer addSublayer:repL];
    
    //创建CAlayer
    CALayer *layer = [CALayer layer];
    layer.backgroundColor= [UIColor redColor].CGColor;
    CGFloat w = 30;
    CGFloat h = 100;
    layer.bounds = CGRectMake(0, 0, w, h);
    layer.position = CGPointMake(0, self.contentViiew.frame.size.height);
    layer.anchorPoint = CGPointMake(0, 1);
    [repL addSublayer:layer];
    
    //复制5份
    repL.instanceCount = 5;
    //平移
    repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);

CAShapeLayer--形状图层

//形状图层
        CAShapeLayer * slayer = [CAShapeLayer layer];
        slayer.path = path.CGPath;
        slayer.backgroundColor = [UIColor redColor].CGColor;
        slayer.fillColor = [UIColor clearColor].CGColor;
        slayer.lineCap = kCALineCapRound;
        slayer.lineJoin = kCALineJoinRound;
        slayer.strokeColor = self.color.CGColor;
        slayer.lineWidth = path.lineWidth;
        [self.layer addSublayer:slayer];
上一篇 下一篇

猜你喜欢

热点阅读