美文网首页
Core Animation

Core Animation

作者: Fat_Blog | 来源:发表于2021-03-02 19:02 被阅读0次
  • Core Animation是可以用在Mac OS X 和iOS平台的
  • 动画的执行是在后台操作的,不会阻塞主线程
  • 直接作用在CALayer,而不是UIVIew
image.png

CAKeyframeAnimation

  • 可以给动画添加路径 (path)
 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    //贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    //起点
    [path moveToPoint:CGPointMake(50, 50)];
    //连线
    [path addLineToPoint:CGPointMake(300, 50)];
    anim.duration = 0.5;
    //动画保持最后一个状态
    anim.fillMode = kCAFillModeForwards;
    anim.path = path.CGPath;
    //动画执行完不会被自动删除
    anim.removedOnCompletion = NO;
    [self.imageV.layer addAnimation:anim forKey:nil];
  • 给动画设置多个变化的值
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"transform.rotation";
    anim.values = @[@angleZRad(-5),@angleZRad(5)];
    //自动反转
    anim.autoreverses = YES;
    anim.repeatCount = MAXFLOAT;
    [self.imageV.layer addAnimation:anim forKey:nil];

CABasicAnimation

  • 可以设置动画变化值的起点 终点
CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.scale";
    anim.toValue = @0;
    anim.duration = 1;
    //设置自动反转
    anim.autoreverses = YES;
    //设置动画无线执行
    anim.repeatCount = MAXFLOAT;
    [self.imageView.layer addAnimation:anim forKey:nil];

CATransition

  • 设置转场动画
    参数type的类型
  • fade
    交叉淡化过渡
    不支持过渡方向
    对应的常量为kCATransitionFade
  • push
    新视图把旧视图推出去
    对应的常量为kCATransitionPush
  • moveIn
    新视图移到旧视图上面
    对应的常量为kCATransitionMoveIn
  • reveal
    将旧视图移开显示下边的新视图
    对应的常量为kCATransitionReveal
  • cube
    立方体反转效果
  • oglFlip
    上下左右反转效果
  • suckEffect
    收缩效果,如一块布被抽走
    不支持过渡方向
  • rippleEffect
    滴水效果
    不支持过渡方向
  • pageCurl
    向上翻页效果
  • pageUnCurl
    向下翻页效果
  • cameraIrisHollowOpen
    相机镜头打开效果
    不支持过渡方向
  • cameraIrisHollowClose
    相机镜头关闭效果
    不支持过渡方向
 //转场代码跟转场动画必须连在一起
    CATransition *anim = [CATransition animation];
    anim.type = @"push";
    self.i++;
    NSString *image = [NSString stringWithFormat:@"%d",self.i];
    self.imageV.image = [UIImage imageNamed:image];
    [self.imageV.layer addAnimation:anim forKey:nil];

CAAnimationGroup

  • 动画组 可以同时设置多个动画
CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position.y";
    anim.toValue = @400;
    
    CABasicAnimation *anim2 = [CABasicAnimation animation];
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @0.5;
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[anim,anim2];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    
    [self.imageV.layer addAnimation:group forKey:nil];

UIView动画跟核心动画的区别

  • 核心动画只作用在layer上
  • 核心动画看到的只是假象,并没有改变View的真实位置

什么时候选择核心动画

  • View不需要跟用户交互
  • 要给动画设置变化的路径
  • 实现转场动画(核心动画的转场类型更多)

demo https://github.com/KK177/Animation

相关文章

网友评论

      本文标题:Core Animation

      本文链接:https://www.haomeiwen.com/subject/rqddfltx.html