iOS 動畫之CALayer動畫

CALayer動畫
CAPropertyAnimation 抽象類
一般使用下面這兩個方法進行實現動畫
CABasicAnimation 基礎的動畫 更改大小 旋轉 等
CAKeyframeAnimation 主要按軌跡移動 更改位置 比如 執行一組動畫時 使用 背景顏色

CABasicAnimation 基本步驟
1.創建一個基礎動畫
// 以旋轉爲例

 // 參數 要實現動畫的屬性 keyPath
    // 注意keyPath一定不要拼錯
    // 咱們要更改的根據加transform.rotation.x
    // 形變屬性下 弧度的X軸 根據X軸旋轉
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];

2 設置屬性的變化 到什麼值

 // tovalue 需要一個對象類型 即NSNumber 或 NSValue
    animation.toValue = [NSNumber numberWithFloat:0. 5];

3 設置動畫時間 重複次數

 animation.duration = 1;
  animation.repeatDuration = 3;

4.把設置好的動畫添加到layer上

 // 參數2 添加到動畫的標識
    [self.MyView.layer addAnimation:animation forKey:@"transform.rotation.x"];

CAKeyframeAnimation 基本步驟
// 以晃動爲例
1.創建動畫

 NSStringFromCGPoint(self.MyView.layer.position));
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];

2.變成對象

 NSNumber *num1 = [NSNumber numberWithFloat:self.MyView.layer.position.x - 50];
    NSNumber *num2 = [NSNumber numberWithFloat:self.MyView.layer.position.x ];
    NSNumber *num3 = [NSNumber numberWithFloat:self.MyView.layer.position.x + 50];
   NSNumber *num4 = [NSNumber numberWithFloat:self.MyView.layer.position.x ];
    NSNumber *num5 = [NSNumber numberWithFloat:self.MyView.layer.position.x - 50];

3.賦值values 需要一個數組

 animation.values = @[num1, num2, num3, num4, num5];

4.設置動畫時間

animation.duration = 0.5;
    animation.repeatCount = 4;

5.添加動畫

 [self.MyView.layer addAnimation:animation forKey:@"position"];

};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章