forked from JWOpenSources/STBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTSortView.m
More file actions
206 lines (168 loc) · 6.86 KB
/
STSortView.m
File metadata and controls
206 lines (168 loc) · 6.86 KB
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
200
201
202
203
204
205
206
//
// STSortView.m
// STBasic
//
// Created by SunJiangting on 13-11-2.
// Copyright (c) 2013年 SunJiangting. All rights reserved.
//
#import "STSortView.h"
const NSInteger kSTSortElementTagOffset = 100;
@interface STSortView ()
@property (nonatomic, strong) NSMutableArray * elementViewArray;
@property (nonatomic, strong) UIView * baseline1;
@property (nonatomic, strong) UIView * baseline2;
@property (nonatomic, assign) CGFloat baseWidth;
@property (nonatomic, weak) UIView * cacheView;
- (UIView *) viewAtIndex:(NSInteger) index;
- (CGFloat) frameXAtIndex:(NSInteger) index;
@end
@implementation STSortView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.factor = 5;
self.elementViewArray = [NSMutableArray arrayWithCapacity:5];
self.baseline1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, CGRectGetHeight(frame))];
self.baseline1.backgroundColor = [UIColor blueColor];
[self addSubview:self.baseline1];
self.baseline2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, CGRectGetHeight(frame))];
self.baseline2.backgroundColor = [UIColor greenColor];
[self addSubview:self.baseline2];
}
return self;
}
- (void) reloadSortDataSource:(NSArray *) dataSource {
[self.elementViewArray enumerateObjectsUsingBlock:^(UIView * subView, NSUInteger idx, BOOL *stop) {
[subView removeFromSuperview];
}];
[self.elementViewArray removeAllObjects];
int width = CGRectGetWidth(self.bounds) / dataSource.count;
self.baseWidth = width;
self.baseline1.frame = CGRectMake(0.5 * width, 0, 2, CGRectGetHeight(self.bounds));
self.baseline2.frame = CGRectMake(0.5 * width, 0, 2, CGRectGetHeight(self.bounds));
[dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CGFloat height = [obj intValue] * self.factor;
CGRect frame = CGRectZero;
frame.origin = CGPointMake((idx + 0.5) * width, CGRectGetHeight(self.bounds) - height);
frame.size = CGSizeMake(width - 10, height);
UIView * view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor blackColor];
view.tag = idx + kSTSortElementTagOffset;
[self addSubview:view];
[self.elementViewArray addObject:view];
}];
}
/// 基线表示参考线,用于表示 循环走到了哪个节点
- (void) moveBaseline1ToIndex:(NSInteger) index
duration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
CGFloat left = [self frameXAtIndex:index];
CGRect frame = self.baseline1.frame;
frame.origin.x = left;
[UIView animateWithDuration:duration animations:^{
self.baseline1.frame = frame;
} completion:^(BOOL finished) {
self.baseline1.frame = frame;
if (completion) {
completion(finished);
}
}];
}
- (void) moveBaseline2ToIndex:(NSInteger) index
duration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
CGFloat left = [self frameXAtIndex:index];
CGRect frame = self.baseline2.frame;
frame.origin.x = left;
[UIView animateWithDuration:duration animations:^{
self.baseline2.frame = frame;
} completion:^(BOOL finished) {
self.baseline2.frame = frame;
if (completion) {
completion(finished);
}
}];
}
/// 将某个位置的元素移动到另一个位置 比如 array[toIndex] = array[index]
- (void) moveElementAtIndex:(NSInteger) index
toIndex:(NSInteger) toIndex
duration:(NSTimeInterval) duration
completion:(void (^)(BOOL finished)) completion {
UIView * subview = [self viewAtIndex:index];
CGFloat left = [self frameXAtIndex:toIndex];
CGRect frame = subview.frame;
frame.origin.x = left;
frame.origin.y = CGRectGetHeight(self.bounds) - CGRectGetHeight(subview.bounds);
subview.tag = toIndex + kSTSortElementTagOffset;
self.cacheView.tag = index + kSTSortElementTagOffset;
[UIView animateWithDuration:duration animations:^{
subview.frame = frame;
} completion:^(BOOL finished) {
subview.frame = frame;
if (completion) {
completion(finished);
}
}];
}
/// 将某一个位置的元素移动到缓冲区,比如 int temp = array[index]
- (void) moveElementToCacheAtIndex:(NSInteger) index
duration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
UIView * subview = [self viewAtIndex:index];
CGRect frame = subview.frame;
frame.origin.y = 0;
[UIView animateWithDuration:duration animations:^{
subview.frame = frame;
} completion:^(BOOL finished) {
subview.frame = frame;
if (completion) {
completion(finished);
}
}];
self.cacheView = subview;
}
- (void) removeElementFromCacheWithDuration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
CGRect frame = self.cacheView.frame;
frame.origin.y = CGRectGetHeight(self.bounds) - CGRectGetHeight(self.cacheView.frame);
frame.origin.x = [self frameXAtIndex:(self.cacheView.tag - kSTSortElementTagOffset)];
[UIView animateWithDuration:duration animations:^{
self.cacheView.frame = frame;
} completion:^(BOOL finished) {
self.cacheView.frame = frame;
completion(finished);
}];
self.cacheView = nil;
}
/// 交换其中两个元素的位置 比如 int temp = array[index1];array[index1] = array[index2];array[index2] = temp;
- (void) exchangeElementAtIndex:(NSInteger) index1
withElementAtIndex:(NSInteger) index2
duration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
UIView * subview1 = [self viewAtIndex:index1];
UIView * subview2 = [self viewAtIndex:index2];
CGRect frame1 = subview1.frame;
CGRect frame2 = subview2.frame;
frame1.origin.x = [self frameXAtIndex:index2];
frame2.origin.x = [self frameXAtIndex:index1];
subview1.tag = index2 + kSTSortElementTagOffset;
subview2.tag = index1 + kSTSortElementTagOffset;
[UIView animateWithDuration:duration animations:^{
subview1.frame = frame1;
subview2.frame = frame2;
} completion:^(BOOL finished) {
subview1.frame = frame1;
subview2.frame = frame2;
if (completion) {
completion(finished);
}
}];
}
- (UIView *) viewAtIndex:(NSInteger) index {
return [self viewWithTag:(index + kSTSortElementTagOffset)];
}
- (CGFloat) frameXAtIndex:(NSInteger) index {
return (index + 0.5) * self.baseWidth;
}
@end