核心动画

核心动画--->CoreAnimation导入<QuartzCore/QuartzCore.h>
  CAAnimation是个抽象类,应该使用他具体的子类。
    一、基本动画
  1>anmi.removedOnCompletion = NO;//如果退出到后台,动画要继续执行的话,需要用将这个属性设置为NO,默认是YES;
2>#pragma mark 平移
  - (void)moveTo:(CGPoint)to
  {
  //1.实例化一个基本动画
  CABasicAnimation *anmi = [CABasicAnimationanimationWithKeyPath:@"position"];
  //2.toValue&fromValue
  anmi.toValue = [NSValue valueWithCGPoint:to];
  //3.动画时长
  anmi.duration = 2.0f;
  //速度函数
  anmi.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  //4. 启动动画
  [_redView.layer addAnimation:anmi forKey:nil];
  }
3>#pragma mark 缩放
  - (void)scale
  {
  CABasicAnimation *anmi = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  anmi.toValue = @(2.0);
  anmi.repeatCount = 3;
  anmi.duration = 1.0f;
  [_redView.layer addAnimation:anmi forKey:nil];
  }
4>#pragma mark 旋转
  - (void)rotate
  {  
  CABasicAnimation *anmi = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  anmi.toValue = @(M_PI * 2);
  anmi.repeatCount = HUGE_VALF;
  anmi.duration = 1.0f;
  [_redView.layer addAnimation:anmi forKey:nil];
  }
5>#pragma mark 暂停CALayer的动画
  -(void)pauseLayer:(CALayer*)layer
  {
  CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
  // 让CALayer的时间停止走动
  layer.speed = 0.0;
  // 让CALayer的时间停留在pausedTime这个时刻
  layer.timeOffset = pausedTime;
  }

6>#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;
  }

    二、.关键帧动画
#pragma mark 设置关键帧动画的values ----->平移到目标点
  - (void)moveTo:(CGPoint)to
  {
  CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];//初始化关键针动画,指定keyPath
  NSValue *p1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
  NSValue *p2 = [NSValue valueWithCGPoint:self.center];
  NSValue *p3 = [NSValue valueWithCGPoint:to];
  anima.values = @[p1,p2,p3]; //设置values
  anima.duration = 3.0f;
  anima.removedOnCompletion = NO; //清除之前的动画属性
  anima.fillMode = kCAFillModeForwards; //设置填充模式
  [self.layer addAnimation:anima forKey:nil];
  }
#pragma mark 设置关键帧动画的path--- ----->按照矩路径进行动画
  - (void)moveToRectBy:(CGPoint)by
  {
  CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  //1.创建路径
  CGMutablePathRef path = CGPathCreateMutable();
  //2.指定Rect
  CGRect rect = CGRectMake(self.center.x, self.center.y, by.x - self.center.x, by.y - self.center.y);
  //3.将Rect添加到路径
  CGPathAddRect(path, NULL, rect);
  //4.设置路径
  anima.path = path;
  [self.layer addAnimation:anima forKey:nil];
  //5.释放路径
  CGPathRelease(path);
  }
#pragma mark 设置关键帧动画的path--- ----->曲线路径动画
  - (void)moveToRectto:(CGPoint)to
  {
  CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  //1.创建路径
  CGMutablePathRef path = CGPathCreateMutable();
  CGPathMoveToPoint(path, NULL, self.center.x, self.center.y);
  CGPathAddQuadCurveToPoint(path, NULL, 0, 0, to.x, to.y);
  //2.设置路径
  anima.path = path;
  [self.layer addAnimation:anima forKey:nil];
  //3.释放路径
  CGPathRelease(path);
  }
#pragma mark 摇晃动画
  - (void)shake
  {
  CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
  CGFloat angle = M_PI_4 / 6;
  NSArray *angles = @[@(-angle),@(angle),@(-angle)];
  anima.values = angles;
  anima.duration = 0.1f;
  anima.repeatCount = HUGE_VALF;
  [self.layer addAnimation:anima forKey:nil];
  }
    三、动画组
   *群组动画可以将一组动画集成在一起,并发执行,产生更加复杂的动画效果。不能修改同一个KeyPath
  - (void)groupDemo
  {
  //1、初始化一个群组动画
  CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
  //2、透明度动画
  CABasicAnimation *alpha = [CABasicAnimation animationWithKeyPath:@"opacity"];
  alpha.fromValue = @(1.0);
  alpha.toValue = @(0.2);
  //3、路径动画
  CAKeyframeAnimation *path = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  CGMutablePathRef paths = CGPathCreateMutable();
  CGPathAddEllipseInRect(paths, NULL, CGRectMake(100, 100, 200, 300));
  path.path = paths;
  //4、将动画添加到组动画中
  group.animations = @[path,alpha];
  [_myView.layer addAnimation:group forKey:nil];

  }

原文地址:https://www.cnblogs.com/yinqiang/p/3491411.html