forked from lovesunstar/STBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTHanoiView.m
More file actions
156 lines (134 loc) · 5.11 KB
/
STHanoiView.m
File metadata and controls
156 lines (134 loc) · 5.11 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
//
// STHanoiView.m
// STBasic
//
// Created by SunJiangting on 13-11-3.
// Copyright (c) 2013年 SunJiangting. All rights reserved.
//
#import "STHanoiView.h"
@interface STHanoiView ()
@property (nonatomic, strong) UIView * columnView1;
@property (nonatomic, strong) UIView * columnView2;
@property (nonatomic, strong) UIView * columnView3;
@property (nonatomic, assign) CGFloat baseWidth;
@property (nonatomic, assign) CGFloat baseHeight;
@property (nonatomic, strong) NSMutableArray * hanoiViewArray;
@property (nonatomic, strong) NSMutableArray * columnHanoi1;
@property (nonatomic, strong) NSMutableArray * columnHanoi2;
@property (nonatomic, strong) NSMutableArray * columnHanoi3;
@property (nonatomic, assign) CGFloat hanoiHeight;
@property (nonatomic, assign) CGFloat verticalMargin;
@end
const NSInteger kSTDiskTagOffset = 100;
@implementation STHanoiView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.baseWidth = CGRectGetWidth(frame) / 3;
self.baseHeight = CGRectGetHeight(frame) * 2 / 3;
self.columnView1 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 0.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)];
self.columnView1.backgroundColor = [UIColor blackColor];
[self addSubview:self.columnView1];
self.columnView2 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 1.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)];
self.columnView2.backgroundColor = [UIColor blackColor];
[self addSubview:self.columnView2];
self.columnView3 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 2.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)];
self.columnView3.backgroundColor = [UIColor blackColor];
[self addSubview:self.columnView3];
self.hanoiViewArray = [NSMutableArray arrayWithCapacity:3];
self.columnHanoi1 = [NSMutableArray arrayWithCapacity:3];
self.columnHanoi2 = [NSMutableArray arrayWithCapacity:3];
self.columnHanoi3 = [NSMutableArray arrayWithCapacity:3];
}
return self;
}
- (void) reloadHanoiView {
[self.hanoiViewArray enumerateObjectsUsingBlock:^(UIView * subview, NSUInteger idx, BOOL *stop) {
[subview removeFromSuperview];
}];
[self.hanoiViewArray removeAllObjects];
[self.columnHanoi1 removeAllObjects];
[self.columnHanoi2 removeAllObjects];
[self.columnHanoi3 removeAllObjects];
CGFloat height = MIN(self.baseHeight / _numberOfHanois, 15);
self.hanoiHeight = height;
CGFloat increment = MIN(30, (self.baseWidth - 20) / _numberOfHanois);
for (int i = 0; i < _numberOfHanois; i ++) {
CGFloat width = self.baseWidth - i * increment;
CGFloat left = (self.baseWidth - width) / 2;
CGFloat top = CGRectGetHeight(self.bounds) - (i + 1) * height + 5;
UIView * hanoi = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height - 5)];
hanoi.backgroundColor = [UIColor orangeColor];
[self addSubview:hanoi];
[self.hanoiViewArray addObject:hanoi];
[self.columnHanoi1 addObject:hanoi];
}
}
- (void) moveDiskAtIndex:(NSInteger) index
toIndex:(NSInteger) toIndex
duration:(NSTimeInterval) duration
completion:(void(^)(BOOL finished)) completion {
NSMutableArray * array1, * array2 = nil;
switch (index) {
case 0:
array1 = self.columnHanoi1;
break;
case 1:
array1 = self.columnHanoi2;
break;
case 2:
array1 = self.columnHanoi3;
break;
default:
break;
}
switch (toIndex) {
case 0:
array2 = self.columnHanoi1;
break;
case 1:
array2 = self.columnHanoi2;
break;
case 2:
array2 = self.columnHanoi3;
break;
default:
break;
}
UIView * view = [array1 lastObject];
[array1 removeObject:view];
CGPoint center = [self centerForHanoiAtColumn:toIndex];
[array2 addObject:view];
[UIView animateWithDuration:duration animations:^{
view.center = center;
} completion:^(BOOL finished) {
view.center = center;
completion(finished);
}];
}
- (void) setNumberOfHanois:(NSInteger)numberOfHanois {
_numberOfHanois = numberOfHanois;
[self reloadHanoiView];
}
- (CGPoint) centerForHanoiAtColumn:(NSInteger) column {
NSArray * array = nil;
switch (column) {
case 0:
array = self.columnHanoi1;
break;
case 1:
array = self.columnHanoi2;
break;
case 2:
array = self.columnHanoi3;
break;
default:
break;
}
NSInteger count = array.count;
CGFloat centerX = self.baseWidth * (column + 0.5);
CGFloat centerY = CGRectGetHeight(self.bounds) - count * self.hanoiHeight - (self.hanoiHeight + 5) / 2 + 5;
return CGPointMake(centerX, centerY);
}
@end