优化优化,不断优化。昨天成功的将一个AnimationUtils改造成一个RotateAnimation。
AnimationUtils里只有一个方法,+ (void)rotateAnimation,创建旋转动画,当时写他是想以后可以在里面加入类似旋转、翻页、位置动画...。
越来越觉得这样不好,AnimationUtils,从名称上看是个工具类,里面会提供什么样的方法,从名称上不清楚。既然只有一个rotateAnimation方法,那就改造成RotateAnimation吧。
DuRotateAnimation.h
#import <Foundation/Foundation.h>
@interface DuRotateAnimation : CABasicAnimation
@property (nonatomic, assign) BOOL clockwise;
@end
DuRotateAnimation.m
#import "DuRotateAnimation.h"
@interface DuRotateAnimation ()
@end
@implementation DuRotateAnimation
- (id)init
{
self = [super init];
if (self != nil) {
_clockwise = YES;
self.keyPath = @"transform";
self.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
self.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(_clockwise ? M_PI_2 : M_PI, 0.0, 0.0, 1.0) ];
self.duration = 0.25;
self.cumulative = YES;
self.repeatCount = CGFLOAT_MAX;
}
return self;
}
- (void)setClockwise:(BOOL)clockwise
{
if (_clockwise != clockwise) {
_clockwise = clockwise;
}
self.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(_clockwise ? M_PI_2 : M_PI, 0.0, 0.0, 1.0) ];
}
@end
1. 从类名就能清楚知道这个类的作用,不需要阅读者思考;
2. 更灵活。可以根据需求定制自己的RotateAnimation;
3. 更方便。一行代码即可创建一个RotateAnimation;