平常我们在加载数据的时候通常用UIActivityIndicatorView, 但是有时产品想改改样式,在此刚好看到几款动画兴许可以当做活动加载器,特此学习下。
学习来源:DTIActivityIndicatorView-Swift,在此用Objective-C模仿一下。
pulse一、圈圈的跳动
#import "PulseView.h"
@interface PulseView ()
@property (nonatomic, strong) UIView *pulseView;
@end
@implementation PulseView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
CGSize pulseSize = CGRectInset(frame, 2.0, 2.0).size;
_pulseView = [[UIView alloc] init];
_pulseView.backgroundColor = [UIColor whiteColor];
_pulseView.frame = CGRectMake(0.0, 0.0, pulseSize.width, pulseSize.height);
_pulseView.layer.cornerRadius = MIN(pulseSize.width,pulseSize.height)/2.0;
[self addSubview:_pulseView];
}
return self;
}
- (void)startAnimating {
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.values = @[@0,@1];
CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.values = @[@1,@0];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 1.0;
group.repeatCount = MAXFLOAT;
group.removedOnCompletion = NO;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.animations = @[scaleAnimation, opacityAnimation];
[self.pulseView.layer addAnimation:group forKey:@"Animation_Pulse"];
}
@end
doublw二、变换的跳动
#import "DoubleBounceView.h"
@interface DoubleBounceView ()
@property (nonatomic, strong) UIView *bounceOutView;
@property (nonatomic, strong) UIView *bounceInView;
@end
@implementation DoubleBounceView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
CGSize size = CGRectInset(frame, 2.0, 2.0).size;
_bounceInView = [self makeBounceViewWithSize:size];
_bounceOutView = [self makeBounceViewWithSize:size];
[self addSubview:_bounceOutView];
[self addSubview:_bounceInView];
}
return self;
}
- (UIView *)makeBounceViewWithSize:(CGSize)size {
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, size.width, size.height);
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = MIN(size.width,size.height)/2.0;
view.layer.opacity = 0.6;
return view;
}
- (void)startAnimating {
CAKeyframeAnimation *scaleInAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleInAnimation.values = @[@0, @1, @0];
scaleInAnimation.removedOnCompletion = NO;
scaleInAnimation.repeatCount = CGFLOAT_MAX;
scaleInAnimation.duration = 2;
scaleInAnimation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
CAKeyframeAnimation *scaleOutAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleOutAnimation.values = @[@1, @0, @1];
scaleOutAnimation.removedOnCompletion = NO;
scaleOutAnimation.repeatCount = MAXFLOAT;
scaleOutAnimation.duration = 2;
scaleOutAnimation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
[self.bounceOutView.layer addAnimation:scaleOutAnimation forKey:@"scaleOutAnimation"];
[self.bounceInView.layer addAnimation:scaleInAnimation forKey:@"scaleInAnimation"];
}
@end
wave.gif三、琴块的变化
#import "WaveView.h"
static const NSInteger kRectCount = 5;
static const CGFloat kSpaceBetweenRect = 3.0;
@interface WaveView ()
@property (nonatomic, strong) UIView *rectView;
@end
@implementation WaveView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_rectView = [[UIView alloc] initWithFrame: frame];
[self addSubview:_rectView];
CGFloat rectWidth = (frame.size.width - kSpaceBetweenRect *(kRectCount-1)) / kRectCount;
for (NSInteger i = 0; i < kRectCount; ++i) {
CALayer *layer = [CALayer layer];
layer.transform = CATransform3DMakeScale(1.0, 0.4, 0.0);
layer.backgroundColor = [UIColor whiteColor].CGColor;
layer.frame = CGRectMake(i * (rectWidth + kSpaceBetweenRect), 0, rectWidth, frame.size.height);
[_rectView.layer addSublayer:layer];
}
}
return self;
}
- (void)startAnimating {
for (NSInteger i = 0; i < kRectCount; ++i) {
CALayer *layer = self.rectView.layer.sublayers[i];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.removedOnCompletion = NO;
animation.repeatCount = MAXFLOAT;
animation.duration = 1.2;
animation.beginTime = CACurrentMediaTime() + i * 0.1;
animation.keyTimes = @[@0.0,@0.2, @0.4,@1.0];
animation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
animation.values = @[
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 0.0)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)]
];
[layer addAnimation:animation forKey:[NSString stringWithFormat:@"wave_animation_%lu",i]];
}
}
@end
Rota.gif四、方块的旋转
#import "RotatingView.h"
@interface RotatingView ()
@property (nonatomic, strong) UIView *planeView;
@end
@implementation RotatingView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
CGSize size = CGRectInset(frame, 2.0, 2.0).size;
_planeView = [[UIView alloc] init];
_planeView.backgroundColor = [UIColor whiteColor];
_planeView.frame = CGRectMake(0.0, 0.0, size.width, size.height);
[self addSubview:_planeView];
}
return self;
}
CATransform3D transformPlane(CGFloat perspective, CGFloat angle,
CGFloat x, CGFloat y, CGFloat z) {
CATransform3D translation = CATransform3DIdentity;
translation.m34 = perspective;
return CATransform3DRotate(translation, angle, x, y, z);
}
- (void)startAnimating {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.removedOnCompletion = NO;
animation.repeatCount = MAXFLOAT;
animation.duration = 2;
animation.keyTimes = @[@0.0,@0.45,@0.95];
animation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
animation.values = @[
[NSValue valueWithCATransform3D:transformPlane(1.0/120.0, 0.0, 0.0, 0.0, 0.0)],
[NSValue valueWithCATransform3D:transformPlane(1.0/120.0, (CGFloat)M_PI, 0.0, 1.0, 0.0)],
[NSValue valueWithCATransform3D:transformPlane(1.0/120.0, (CGFloat)M_PI, 0.0, 0.0, 1.0)]
];
[self.planeView.layer addAnimation:animation forKey:@"Animation_Plane"];
}
@end
Chasing.gif五、二圆旋转飞
#import "ChasingView.h"
@interface ChasingView ()
@property (nonatomic, strong) UIView *dotOneView;
@property (nonatomic, strong) UIView *dotTwoView;
@end
@implementation ChasingView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.layer.shouldRasterize = YES;
self.backgroundColor = [UIColor lightGrayColor];
CGFloat sizeWidth = frame.size.width * 3/5;
_dotOneView = [self makeDotViewWithSize:CGRectMake((frame.size.width - sizeWidth)/2.0, 1.0, sizeWidth, sizeWidth)];
_dotTwoView = [self makeDotViewWithSize:CGRectMake((frame.size.width - sizeWidth)/2.0, frame.size.height = sizeWidth, sizeWidth, sizeWidth)];
[self addSubview:_dotOneView];
[self addSubview:_dotTwoView];
}
return self;
}
- (UIView *)makeDotViewWithSize:(CGRect)rect {
UIView *view = [[UIView alloc] init];
view.frame = rect;
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = MIN(rect.size.width,rect.size.height)/2.0;
return view;
}
- (void)startAnimating {
CABasicAnimation *ratotionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
ratotionAnimation.fromValue = @0;
ratotionAnimation.toValue = @(2 * M_PI);
ratotionAnimation.removedOnCompletion = NO;
ratotionAnimation.repeatCount = MAXFLOAT;
ratotionAnimation.duration = 2.0;
ratotionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.layer addAnimation:ratotionAnimation forKey:@"chasing_rotation"];
CAKeyframeAnimation *scaleOneAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleOneAnimation.values = @[@1, @0, @1];
scaleOneAnimation.removedOnCompletion = NO;
scaleOneAnimation.repeatCount = CGFLOAT_MAX;
scaleOneAnimation.duration = 2;
scaleOneAnimation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
CAKeyframeAnimation *scaleTwoAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleTwoAnimation.values = @[@0, @1, @0];
scaleTwoAnimation.removedOnCompletion = NO;
scaleTwoAnimation.repeatCount = CGFLOAT_MAX;
scaleTwoAnimation.duration = 2;
scaleTwoAnimation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
[self.dotOneView.layer addAnimation:scaleOneAnimation forKey:@"chasing_one_scale"];
[self.dotTwoView.layer addAnimation:scaleTwoAnimation forKey:@"chasing_two_scale"];
}
@end
threePoint.gif六、三个点
#import "SpotifyView.h"
static const NSInteger kCircleCount = 3;
@interface SpotifyView ()
@property (nonatomic, strong) UIView *circleView;
@end
@implementation SpotifyView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_circleView = [[UIView alloc] initWithFrame: frame];
[self addSubview:_circleView];
CGFloat circleWidth = frame.size.width / (kCircleCount*2 + 1);
CGFloat posY = (frame.size.height - circleWidth)/2;
for (NSInteger i = 0; i < kCircleCount; ++i) {
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor whiteColor].CGColor;
layer.cornerRadius = circleWidth/2;
layer.frame = CGRectMake(circleWidth + i*(circleWidth*2), posY, circleWidth, circleWidth);
[_circleView.layer addSublayer:layer];
}
}
return self;
}
- (void)startAnimating {
for (NSInteger i = 0; i < kCircleCount; ++i) {
CALayer *layer = self.circleView.layer.sublayers[i];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
animation.removedOnCompletion = NO;
animation.repeatCount = MAXFLOAT;
animation.duration = 1.2;
animation.values = @[@1.0, @1.7, @1.0, @1.0];
animation.beginTime = CACurrentMediaTime() + i * 0.2;
animation.keyTimes = @[@0.0,@0.2, @0.4,@1.0];
animation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
[layer addAnimation:animation forKey:[NSString stringWithFormat:@"spotify_animation_%lu",i]];
}
}
@end
网易刷新圈圈七、网易刷新圈圈(额外需要图片的)
#import "ShowView.h"
static const CGFloat kCicleEdge = 7.0f;
@interface ShowView ()
@property (nonatomic, strong) CALayer *ballLayer;
@property (nonatomic, strong) CALayer *circleLayer;
@end
@implementation ShowView
- (instancetype)initWithFrame:(CGRect)frame {
if ([super initWithFrame:frame]) {
UIImage *ballImage = [UIImage imageNamed:@"ball"]; // size == 30 * 30
UIImage *circleImage = [UIImage imageNamed:@"circle"]; // size == 100 * 30
_ballLayer = [CALayer layer];
_ballLayer.frame = CGRectMake(0, 0, ballImage.size.width - kCicleEdge, ballImage.size.height - kCicleEdge);
_ballLayer.position = CGPointMake(self.center.x, _ballLayer.position.y);
_ballLayer.contents = (id)ballImage.CGImage;
_circleLayer = [CALayer layer];
_circleLayer.frame = CGRectMake(0, 0, circleImage.size.width, circleImage.size.height);
_circleLayer.position = CGPointMake(self.center.x, _ballLayer.position.y - kCicleEdge);
_circleLayer.contents = (id)circleImage.CGImage;
[self.layer addSublayer:_ballLayer];
[self.layer insertSublayer:_circleLayer below:_ballLayer];
}
return self;
}
// 增加动画
- (void) startAnimating {
// 变大
CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
enlarge.duration = 0.5f;
enlarge.repeatCount = 1.0f;
enlarge.removedOnCompletion = NO;
enlarge.fromValue = @(0.5f);
enlarge.toValue = @(1.0f);
// 变小
CABasicAnimation *decrease = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
decrease.duration = 0.5f;
decrease.repeatCount = 1.0f;
decrease.removedOnCompletion = NO;
decrease.beginTime = 0.5f;
decrease.fromValue = @(1.0f);
decrease.toValue = @(0.5f);
// 位置
CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:@"position"];
position.duration = 1.0f;
position.repeatCount = MAXFLOAT;
position.fromValue =[NSValue valueWithCGPoint:self.circleLayer.position];
position.toValue = [NSValue valueWithCGPoint:CGPointMake(self.circleLayer.position.x, self.circleLayer.position.y + self.circleLayer.frame.size.height)];
// 将三组动画组合成
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 1;
group.repeatCount = MAXFLOAT;
group.removedOnCompletion = NO;
group.autoreverses = YES;
group.animations = @[enlarge, decrease, position];
[self.circleLayer addAnimation:group forKey:@"Animation"];
}
// 移除动画
- (void)removeAnimation {
[self.circleLayer removeAnimationForKey:@"Animation"];
}
@end
PS:上述尝试实现,都是直接用 frame 的!用自动布局尝试直接修改里面的chu
重点是看
startAnimating
中的实现!
PS: 另外同时学习 Swift ,直接去看 DTIActivityIndicatorView-Swift 里面的实现是很有必要,虽说好久没更新啦。
同时通过上面几组的学习尝试,更加了体会到了一般动画的构成。
- CALayer === > Who 事件是谁做 ;
- CAAnimation ===> How 事件怎么做;
- AddAnimation ===> Add 将谁做和做怎么结合起来。
虽说上述几组动画都是一些基本的简单动画,但是任何复杂的动画都是由简单动画构成,所以再次提醒自己动画并没有想象中那么难!
网友评论