CABasicAnimation的简单使用

话不多说,直接上代码,里面该有的注释都有了,一看就懂,多敲几遍就行了

// 平移动画
- (void)translationAnimation{
  
  UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 50, 50)];
  [self.view addSubview:kLView];
  
  kLView.layer.borderColor = [UIColor yellowColor].CGColor;
  kLView.layer.borderWidth = 2;
  kLView.backgroundColor = [UIColor redColor];
  
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
  
  animation.duration = 2;
  
  CGFloat width = self.view.frame.size.width;
  animation.toValue = [NSValue valueWithCGPoint:CGPointMake(width - 50, 0)];
  
  // 指定动画重复多少圈是累加的
  animation.cumulative = 2;
  
  // 动画完成后不自动删除
  animation.removedOnCompletion = NO;
  
  // 设置移动的效果为快如快出的效果
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  // 设置无限循环动画
  animation.repeatCount = HUGE_VALF;
  
  // 设置动画完成后,自动以动画的回到原点
  animation.autoreverses = YES;
  
  // 设置东环完成后,返回到原点
  animation.fillMode = kCAFillModeBackwards;
  
  [kLView.layer addAnimation:animation forKey:@"transform.translation"];
}

// 旋转动画


- (void)rotationAnimation{
  UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 240, 50, 50)];
  [self.view addSubview:kLView];
  
  kLView.layer.borderColor = [UIColor yellowColor].CGColor;
  kLView.layer.borderWidth = 2;
  kLView.backgroundColor = [UIColor redColor];
  
  // 这里使用的KVC的思想
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
  
  animation.duration = 0.01;

  // z轴旋转180度
  CATransform3D transform3d =  CATransform3DMakeRotation(3.1415, 0, 0, 180);
  animation.toValue = [NSValue valueWithCATransform3D:transform3d];
  
  animation.cumulative = YES;
  animation.removedOnCompletion = NO;
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animation.repeatCount = HUGE_VALF;
  animation.autoreverses = YES;
  animation.fillMode = kCAFillModeBackwards;
  [kLView.layer addAnimation:animation forKey:@"transform"];
  
}



// 缩放动画
- (void)scaleAnimation{
  
  UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, 50, 50)];
  [self.view addSubview:kLView];
  
  kLView.layer.borderColor = [UIColor yellowColor].CGColor;
  kLView.layer.borderWidth = 2;
  kLView.backgroundColor = [UIColor redColor];
  
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  
  animation.duration = 2;
  animation.fromValue = @(1);
  animation.toValue = @(0);
  animation.removedOnCompletion = NO;
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animation.repeatCount = HUGE_VALF;
  animation.autoreverses = YES;
  animation.fillMode = kCAFillModeBackwards;
  
  [kLView.layer addAnimation:animation forKey:@"transform.scale"];
  
  
}


// 闪烁动画
- (void)flashAnimation
{
  UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 50, 50)];
  [self.view addSubview:kLView];
  
  kLView.layer.borderColor = [UIColor yellowColor].CGColor;
  kLView.layer.borderWidth = 2;
  kLView.backgroundColor = [UIColor redColor];
  
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  
  animation.duration = 0.001;
  animation.fromValue = @(1);
  animation.toValue = @(0);
  
  animation.removedOnCompletion = NO;
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animation.repeatCount = HUGE_VALF;
  animation.autoreverses = YES;
  animation.fillMode = kCAFillModeBackwards;
  
  [kLView.layer addAnimation:animation forKey:@"opacity"];
  
}

// 路径动画
- (void)routeAnimation{
  UIView *kLView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  kLView.layer.borderWidth = 2;
  kLView.layer.borderColor = [UIColor yellowColor].CGColor;
  kLView.backgroundColor = [UIColor redColor];
  
  [self.view addSubview:kLView];
  
  // 添加动画
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
  
  
  /**
   *
      Both fromValue and toValue are non-nil. Interpolates between fromValue and toValue.
   
      fromValue and byValue are non-nil. Interpolates between fromValue and (fromValue + byValue).
   
      byValue and toValue are non-nil. Interpolates between (toValue - byValue) and toValue.
   
   看到这里是不是有点晕,莫怕,带我慢慢说来 ,假设我们都是用fromValue和toValue来进行处理,
   当程序中使用的是fromValue和toValue时,那么fromValue = fromValue,
   toValue = toValue,
   如果程序中是fromValue和byValue,那么fromValue = fromValue ,toValue = (fromValue + byValue);
   如果程序中是toValue和byValue,那么fromValue = (toValue - byValue),toValue = toValue;
   */
  
  
  // 起点 这个值是指position,也就是layer的中心值
  animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
  // 终点 这个值是指position,也就是layer的中心值
  animation.toValue   = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 50, self.view.bounds.size.width - 100)];
  animation.byValue  = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 50, self.view.bounds.size.width - 100)];
  
  // 线性动画
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  animation.removedOnCompletion = NO;
  animation.duration = 2;
  // 这里是播放速率 1表示正常播放,小于1则是慢速,大于1则是快速
  animation.speed = 0.5;
  animation.beginTime = 0;
  animation.repeatCount = HUGE_VALF;
  animation.autoreverses = YES;
  animation.fillMode = kCAFillModeBackwards;
  
  [kLView.layer addAnimation:animation forKey:@"position"];
  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值