forked from BigShow1949/BigShow1949
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path560ff376d7c8e161fca6d1916eea5fd2d5ca54da.svn-base
201 lines (152 loc) · 5.28 KB
/
560ff376d7c8e161fca6d1916eea5fd2d5ca54da.svn-base
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// DBSphereView.m
// sphereTagCloud
//
// Created by Xinbao Dong on 14/8/31.
// Copyright (c) 2014年 Xinbao Dong. All rights reserved.
//
#import "DBSphereView.h"
#import "DBMatrix.h"
@interface DBSphereView() <UIGestureRecognizerDelegate>
@end
@implementation DBSphereView
{
NSMutableArray *tags;
NSMutableArray *coordinate;
DBPoint normalDirection;
CGPoint last;
CGFloat velocity;
CADisplayLink *timer;
CADisplayLink *inertia;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self addGestureRecognizer:gesture];
}
return self;
}
#pragma mark - initial set
- (void)setCloudTags:(NSArray *)array
{
tags = [NSMutableArray arrayWithArray:array];
coordinate = [[NSMutableArray alloc] initWithCapacity:0];
for (NSInteger i = 0; i < tags.count; i ++) {
UIView *view = [tags objectAtIndex:i];
view.center = CGPointMake(self.frame.size.width / 2., self.frame.size.height / 2.);
}
CGFloat p1 = M_PI * (3 - sqrt(5));
CGFloat p2 = 2. / tags.count;
for (NSInteger i = 0; i < tags.count; i ++) {
CGFloat y = i * p2 - 1 + (p2 / 2);
CGFloat r = sqrt(1 - y * y);
CGFloat p3 = i * p1;
CGFloat x = cos(p3) * r;
CGFloat z = sin(p3) * r;
DBPoint point = DBPointMake(x, y, z);
NSValue *value = [NSValue value:&point withObjCType:@encode(DBPoint)];
[coordinate addObject:value];
CGFloat time = (arc4random() % 10 + 10.) / 20.;
[UIView animateWithDuration:time delay:0. options:UIViewAnimationOptionCurveEaseOut animations:^{
[self setTagOfPoint:point andIndex:i];
} completion:^(BOOL finished) {
}];
}
NSInteger a = arc4random() % 10 - 5;
NSInteger b = arc4random() % 10 - 5;
normalDirection = DBPointMake(a, b, 0);
[self timerStart];
}
#pragma mark - set frame of point
- (void)updateFrameOfPoint:(NSInteger)index direction:(DBPoint)direction andAngle:(CGFloat)angle
{
NSValue *value = [coordinate objectAtIndex:index];
DBPoint point;
[value getValue:&point];
DBPoint rPoint = DBPointMakeRotation(point, direction, angle);
value = [NSValue value:&rPoint withObjCType:@encode(DBPoint)];
coordinate[index] = value;
[self setTagOfPoint:rPoint andIndex:index];
}
- (void)setTagOfPoint: (DBPoint)point andIndex:(NSInteger)index
{
UIView *view = [tags objectAtIndex:index];
view.center = CGPointMake((point.x + 1) * (self.frame.size.width / 2.), (point.y + 1) * self.frame.size.width / 2.);
CGFloat transform = (point.z + 2) / 3;
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, transform, transform);
view.layer.zPosition = transform;
view.alpha = transform;
if (point.z < 0) {
view.userInteractionEnabled = NO;
}else {
view.userInteractionEnabled = YES;
}
}
#pragma mark - autoTurnRotation
- (void)timerStart
{
timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(autoTurnRotation)];
[timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)timerStop
{
[timer invalidate];
timer = nil;
}
- (void)autoTurnRotation
{
for (NSInteger i = 0; i < tags.count; i ++) {
[self updateFrameOfPoint:i direction:normalDirection andAngle:0.002];
}
}
#pragma mark - inertia
- (void)inertiaStart
{
[self timerStop];
inertia = [CADisplayLink displayLinkWithTarget:self selector:@selector(inertiaStep)];
[inertia addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)inertiaStop
{
[inertia invalidate];
inertia = nil;
[self timerStart];
}
- (void)inertiaStep
{
if (velocity <= 0) {
[self inertiaStop];
}else {
velocity -= 70.;
CGFloat angle = velocity / self.frame.size.width * 2. * inertia.duration;
for (NSInteger i = 0; i < tags.count; i ++) {
[self updateFrameOfPoint:i direction:normalDirection andAngle:angle];
}
}
}
#pragma mark - gesture selector
- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan) {
last = [gesture locationInView:self];
[self timerStop];
[self inertiaStop];
}else if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint current = [gesture locationInView:self];
DBPoint direction = DBPointMake(last.y - current.y, current.x - last.x, 0);
CGFloat distance = sqrt(direction.x * direction.x + direction.y * direction.y);
CGFloat angle = distance / (self.frame.size.width / 2.);
for (NSInteger i = 0; i < tags.count; i ++) {
[self updateFrameOfPoint:i direction:direction andAngle:angle];
}
normalDirection = direction;
last = current;
}else if (gesture.state == UIGestureRecognizerStateEnded) {
CGPoint velocityP = [gesture velocityInView:self];
velocity = sqrt(velocityP.x * velocityP.x + velocityP.y * velocityP.y);
[self inertiaStart];
}
}
@end