-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpinningDiskView.m
148 lines (119 loc) · 4.17 KB
/
SpinningDiskView.m
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// SpinningDiskView.m
// streetvoice
//
// Created by Stan Tsai on 2014/1/27.
// Copyright (c) 2014年 Stan Tsai. All rights reserved.
//
#import "SpinningDiskView.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>
@interface SpinningDiskView ()
@property (nonatomic, strong) UIImageView *CDImageView;
@end
@implementation SpinningDiskView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initCustomViews];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initCustomViews];
}
return self;
}
- (void)initCustomViews
{
UIImage *border = [self stylishBorder];
self.CDImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self.CDImageView setImage:border];
[self addSubview:self.CDImageView];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 6.0f;
rotationAnimation.RepeatCount = FLT_MAX;
rotationAnimation.cumulative = NO;
rotationAnimation.removedOnCompletion = NO; //No Remove
[self.CDImageView.layer addAnimation:rotationAnimation forKey:@"rotation"];
self.layer.speed = 0.0f;
}
- (void)setImage:(UIImage *)image
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0);
[image drawInRect:self.bounds];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.CDImageView.backgroundColor = [UIColor colorWithPatternImage:resizedImage];
}
- (UIImage *)stylishBorder
{
CGPoint center = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
self.clipsToBounds = YES;
self.layer.cornerRadius = center.x;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [[UIColor grayColor] CGColor];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowRadius = 2.0f;
self.layer.shadowOpacity = 0.6f;
self.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// outside circular
CGContextSetStrokeColorWithColor(context, [[UIColor colorWithWhite:1.0f alpha:0.7f] CGColor]);
CGContextBeginPath(context);
CGContextSetLineWidth(context, self.frame.size.width / 10.0f);
CGContextAddArc(context, center.x, center.y, center.x, 0, 2 * M_PI, 0);
CGContextStrokePath(context);
// inside circular
CGContextBeginPath(context);
CGContextSetLineWidth(context, self.frame.size.width / 40.0f);
CGContextAddArc(context, center.x, center.y, self.frame.size.width / 10.0f, 0, 2 * M_PI, 0);
CGContextStrokePath(context);
// hollow center
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextBeginPath(context);
CGContextSetLineWidth(context, self.frame.size.width / 20.0f);
CGContextAddArc(context, center.x, center.y, self.frame.size.width / 40.0f, 0, 2 * M_PI, 0);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)startRotation
{
self.layer.speed = 1.0f;
self.layer.beginTime = 0.0f;
CFTimeInterval pausedTime = [self.layer timeOffset];
CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
self.layer.beginTime = timeSincePause;
}
- (void)stopRotation
{
CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.layer.speed = 0.0;
self.layer.timeOffset = pausedTime;
}
- (void)toggleRotation
{
if (self.layer.speed == 0.0f) {
[self startRotation];
} else {
[self stopRotation];
}
}
- (void)setRotaion:(BOOL)isRotation
{
if (isRotation) {
[self startRotation];
} else {
[self stopRotation];
}
}
@end