今天看某位同学问时钟效果怎么做,我首先想到的就是view的属性transform里面有旋转角度的方法,当然还有其他的方法,只是我感觉这个最简单(尴尬)。
有两种方式,一个是定时器控制动画,一个是利用core animation的重复动画属性;
定时器
这个不推荐使用,就是利用UIView的封装动画,在以定时器来重复运行动画代码,上代码:
- (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
还有其他的方式来实现,比如阻尼动画