今天看某位同學問時鐘效果怎麼做,我首先想到的就是view的屬性transform裏面有旋轉角度的方法,當然還有其他的方法,只是我感覺這個最簡單(尷尬)。
有兩種方式,一個是定時器控制動畫,一個是利用core animation的重複動畫屬性;
定時器
- (void)animation {
[UIView animateWithDuration:1 animations:^{
_animationView.transform = CGAffineTransformMakeRotation(M_PI * 1 / 4);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
_animationView.transform = CGAffineTransformMakeRotation(-M_PI * 1 / 4);
}];
}];
}
CABasicAnimation
這個使用方法、代碼簡單
- (void)coreAnimationType {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = [NSNumber numberWithFloat:-M_PI / 4];
animation.toValue = [NSNumber numberWithFloat:M_PI / 4];
animation.autoreverses = YES; //設置YES,代表動畫每次重複執行的效果和上一次的相反
animation.duration = 1; //一次動畫的完成時間
animation.repeatCount = NSNotFound; //動畫播放次數 NSNotFound是無限個循環
[_animationView.layer addAnimation:animation forKey:@"animationTransform"];
}
注意:弧度的旋轉利用“transform.rotation”來重建BasicAnimation
還有其他的方式來實現,比如阻尼動畫