簡介
注意事項
CABasicAnimation的屬性
CAMediaTiming協議的屬性
實現步驟
通過storyboard創建需要執行動畫的控件,并擁有它們
@property (weak, nonatomic) IBOutlet UIView *redView;@property (weak, nonatomic) IBOutlet UIImageView *imageView;
添加動畫
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //創建基本動畫屬性 CABasicAnimation *animation = [CABasicAnimation animation]; //指定執行動畫的keyPath屬性 animation.keyPath = @"transform.scale"; //設置動畫的起始值與目的值 animation.fromValue = @1.0; animation.toValue = @0.8; /****配置動畫的行為****/ //以動畫的方式回復到fromValue animation.autoreverses = YES; //單次動畫的執行時間,據說每分鐘心跳72次 animation.duration = 60 / 72; //動畫的重復次數 animation.repeatCount = MAXFLOAT; //取消動畫反彈效果 animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; //將動畫添加到圖層上 [self.redView.layer addAnimation:animation forKey:nil]; [self.imageView.layer addAnimation:animation forKey:nil]; }
執行效果如圖:
若不設置fromValue值,程序將會有Bug,即多次點擊屏幕時動畫停止,如圖
若不取消反彈效果,動畫結束,會瞬間回到fromValue狀態,如圖
若指定autoreverses為YES,會以動畫方式回到fromValue狀態,如圖
新聞熱點
疑難解答