-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropitViewController.m
More file actions
186 lines (157 loc) · 5.06 KB
/
DropitViewController.m
File metadata and controls
186 lines (157 loc) · 5.06 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
//
// DropitViewController.m
// Dropit
//
// Created by easonchen on 15/7/29.
// Copyright (c) 2015年 ___FULLUSERNAME___. All rights reserved.
//
#import "DropitViewController.h"
#import "DropitBehavior.h"
#import "BezierView.h"
@interface DropitViewController ()<UIDynamicAnimatorDelegate>
@property (weak, nonatomic) IBOutlet BezierView *gameView;
@property (strong,nonatomic) UIDynamicAnimator *animator;
@property (strong,nonatomic) DropitBehavior *behavior;
//attach behavior
@property (strong,nonatomic) UIAttachmentBehavior *attachment;
@property (strong,nonatomic) UIView *dropingView;
@end
@implementation DropitViewController
- (UIDynamicAnimator *)animator
{
if (!_animator) {
_animator=[[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
_animator.delegate=self;
}
return _animator;
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator
{
[self removeCompleteRows];
}
- (BOOL)removeCompleteRows
{
NSMutableArray *dropsToRemove=[[NSMutableArray alloc] init];
for (CGFloat y=self.gameView.bounds.size.height-DROP_SIZE.height/2; y>0; y-=DROP_SIZE.height) {
BOOL rowIsComplete=YES;
NSMutableArray *dropsFound=[[NSMutableArray alloc] init];
for (CGFloat x=DROP_SIZE.width/2; x<(self.gameView.bounds.size.width-DROP_SIZE.width/2); x+=DROP_SIZE.width) {
UIView *hitView=[self.gameView hitTest:CGPointMake(x, y) withEvent:NULL];
if ([hitView superview]==self.gameView) {
[dropsFound addObject:hitView];
}
else{
rowIsComplete=NO;
break;
}
}
if (![dropsFound count]) {
break;
}
if (rowIsComplete) {
[dropsToRemove addObjectsFromArray:dropsFound];
}
}
if ([dropsToRemove count]) {
for (UIView *dropView in dropsToRemove) {
[self.behavior removeItem:dropView];
}
[self animateRemoveDrops:dropsToRemove];
}
return NO;
}
static const CGFloat dropsDuration=3.0;
- (void)animateRemoveDrops:(NSArray *)dropsToremove
{
[UIView animateWithDuration:dropsDuration animations:^{
for (UIView *drop in dropsToremove) {
int x=(arc4random()%(int)(self.gameView.bounds.size.width*5))-(int)self.gameView.bounds.size.width*2;
int y=(int)self.gameView.bounds.size.height;
drop.center=CGPointMake(x, -y);
}
} completion:^(BOOL finished) {
[dropsToremove makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
}
- (DropitBehavior *)behavior
{
if (!_behavior) {
_behavior=[[DropitBehavior alloc] init];
[self.animator addBehavior:_behavior];
}
return _behavior;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)tap:(UITapGestureRecognizer *)sender {
[self drop];
}
- (IBAction)grabDrop:(UIPanGestureRecognizer *)sender {
CGPoint gesturePoint=[sender locationInView:self.gameView];
if (sender.state==UIGestureRecognizerStateBegan) {
[self attachDroppingViewToPoint:gesturePoint];
}
else if(sender.state==UIGestureRecognizerStateChanged){
self.attachment.anchorPoint=gesturePoint;
}
else if(sender.state==UIGestureRecognizerStateEnded){
[self.animator removeBehavior:self.attachment];
self.gameView.path=nil;
}
}
- (void)attachDroppingViewToPoint:(CGPoint)anchorPoint
{
if (self.dropingView) {
self.attachment=[[UIAttachmentBehavior alloc] initWithItem:self.dropingView attachedToAnchor:anchorPoint];
UIView *droppingView=self.dropingView;
__weak DropitViewController *weakSelf=self;
self.attachment.action=^{
UIBezierPath *path=[[UIBezierPath alloc] init];
[path moveToPoint:weakSelf.attachment.anchorPoint];
[path addLineToPoint:droppingView.center];
weakSelf.gameView.path=path;
};
self.dropingView=nil;
[self.animator addBehavior:self.attachment];
}
}
static const CGSize DROP_SIZE={40,40};
- (void)drop
{
CGRect frame;
frame.origin=CGPointZero;
frame.size=DROP_SIZE;
int x=(arc4random()%(int)self.gameView.bounds.size.width)/DROP_SIZE.width;
frame.origin.x=x*DROP_SIZE.width;
UIView *dropView=[[UIView alloc]initWithFrame:frame];
dropView.backgroundColor=[self randomColor];
[self.gameView addSubview:dropView];
[self.behavior addItem:dropView];
self.dropingView=dropView;
}
- (UIColor *)randomColor
{
switch (arc4random()%5) {
case 0:
return [UIColor redColor];
case 1:
return [UIColor yellowColor];
case 2:
return [UIColor orangeColor];
case 3:
return [UIColor greenColor];
case 4:
return [UIColor purpleColor];
default:
return [UIColor blackColor];
}
}
@end