1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// KWMLoadingView.m
// iCemarose
//
// Created by 陈荣科 on 2016/11/17.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMLoadingView.h"
@implementation KWMLoadingView
- (instancetype)init{
if (self = [super init]) {
[self initContentView];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self initContentView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self initContentView];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self initContentView];
}
- (void)initContentView{
[[NSBundle mainBundle] loadNibNamed:@"KWMLoadingView" owner:self options:nil];
_vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:_vContent];
}
- (void)startAnimation{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 0.75;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = CGFLOAT_MAX;
[self.ivLoading.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
- (void)stopAnimation{
NSLog(@"停止动画");
//[_customView.layer removeAllAnimations];
[self pauseLayer:_ivLoading.layer];
}
//暂停
- (void)pauseLayer:(CALayer*)layer{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
//恢复
- (void)resumeLayer:(CALayer*)layer{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
@end