IOS之持续旋转动画
其实很简单,我这里做了一下简单的封装,可以直接拿来使用
首先在.h里面设置好顺时针和逆时针旋转的枚举
//设置逆时针和顺时针旋转的枚举
typedef NS_ENUM(NSInteger , Direction){
DirectionReight =0, //顺时针
DirectionLeft //逆时针
};
然后定义实例方法
@interfaceAnimation :NSObject
-(void)setAnimation:(UIImageView*)control WithDirection:(NSUInteger)Direction;
@end
最后在.m中实现实例方法
@implementation Animation
-(void)setAnimation:(UIImageView*)control WithDirection:(NSUInteger)Direction{
//y是根据y轴旋转,x是根据x轴旋转,z是根据z轴旋转
//如果是y, 则transform.rotation.y,如果是x,则transform.rotation.x, 如果是z,则transform.rotation.z
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//这里 M_PI * 2.0 为顺时针旋转,如果想要逆时针旋转可以写成 M_PI * -2.0
if(Direction == DirectionReight){
rotationAnimation.toValue= [NSNumber numberWithFloat:(M_PI*2.0)];
}else if(Direction ==DirectionLeft){
rotationAnimation.toValue= [NSNumber numberWithFloat:(M_PI* -2.0)];
}
rotationAnimation.duration=2.0;
rotationAnimation.removedOnCompletion=NO;
rotationAnimation.repeatCount=MAXFLOAT;
rotationAnimation.fillMode = kCAFillModeForwards;
[control.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
@end
在你需要用都的地方先导入头文件,然后实例化类对象,通过类对象调用实例就行了,如:
//实例化动画的类
Animation* animation =[[Animation alloc]init];
[animation setAnimation:imageview WithDirection:DirectionLeft];
到这里就成功了