iOS中动画的类型

一. 实现动画基本步骤

  • 创建动画对象
  • 设置动画属性
  • 把动画对象添加到某个 CALayer 对象上
  • 需要停止动画:可以调用 remove 方法移除动画

二. iOS 中核心动画种类

  1. 基本动画(CABasicAnimation)

    • 平移动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
    // 动画持续1秒
    anim.duration = 1;
    // 因为CGPoint是结构体,所以用NSValue包装成一个OC对
    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    [layer addAnimation:anim forKey:@"MyAnim"];
    // 通过MyAnim可以取回相应的动画对象,比如用来中途取消动画
    
    • 缩放动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
    

// 没有设置fromValue说明当前状态作为初始值
// 宽度(width)变为原来的2倍,高度(height)变为原来的1.5倍
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];
anim.duration = 1;
[layer addAnimation:anim forKey:nil];

```

* 旋转动画
```objc
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
// 这里是以向量(1, 1, 0)为轴,旋转π/2弧度(90°)
// 如果只是在手机平面上旋转,就设置向量为(0, 0, 1),即Z轴
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 1, 1, 0)];
anim.duration = 1;
[layer addAnimation:anim forKey:nil];

// 1. 创建核心动画的对象
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
// 2. 设置核心动画的操作
anim.keyPath = @"transform.rotation";
// 从哪
anim.fromValue = @(100);
// 到哪
anim.toValue = @(300);
anim.byValue = @(M_PI_4);
// 动画完毕以后不回到原来的位置
anim.fillMode = kCAFillModeForwards;
anim.removedOnCompletion = NO;
// 3. 添加到layer上
[self.layer addAnimation:anim forKey:nil];

```
  1. 关键帧动画(CAKeyframeAnimation)

    • 关键帧动画:有两种形式
      • 第一种是values的形式,
      • 第二种是path路径的形式;
  2. 组动画(CAAnimationGroup)

    • 组动画: CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行
    • 属性解析:animations:用来保存一组动画对象的NSArray
      默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间
  3. 转场动画(CATransition)

    • 转场动画: CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果

除了核心动画之外另外还有

  • 图片帧动画:具体如下UIImageView可以让一系列的图片在特定的时间内按顺序显示

  • 相关属性解析:

    • animationImages:要显示的图片(一个装着UIImage的NSArray)
    • animationDuration:完整地显示一次animationImages中的所有图片所需的时间
    • animationRepeatCount:动画的执行次数(默认为0,代表无限循环)相关方法解析:
// 动画的一些操作
- (void)startAnimating; 开始动画
- (void)stopAnimating;  停止动画
- (BOOL)isAnimating;  是否正在运行动画

未完待续.........

你可能感兴趣的:(iOS中动画的类型)