-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDropitViewController.m
More file actions
181 lines (158 loc) · 5.34 KB
/
Copy pathDropitViewController.m
File metadata and controls
181 lines (158 loc) · 5.34 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
//
// DropitViewController.m
// Dropit
//
// Created by Lokesh Basu on 31/03/14.
// Copyright (c) 2014 IIT Roorkee. All rights reserved.
//
#import "DropitViewController.h"
#import "DropitBehavior.h"
#import "BezierPathView.h"
@interface DropitViewController () <UIDynamicAnimatorDelegate>
@property (weak, nonatomic) IBOutlet BezierPathView *gameView;
@property (strong, nonatomic) UIDynamicAnimator *animator;
@property (strong, nonatomic) DropitBehavior *dropitBehaviour;
@property (strong, nonatomic) UIAttachmentBehavior *attachment;
@property (strong, nonatomic) UIView *droppingView;
@end
@implementation DropitViewController
static const CGSize DROP_SIZE = { 40, 40 };
- (UIDynamicAnimator *)animator
{
if (!_animator) {
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
_animator.delegate = self;
}
return _animator;
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator
{
[self removeCompletedRows];
}
- (BOOL)removeCompletedRows
{
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 *drop in dropsToRemove) {
[self.dropitBehaviour removeItem:drop];
}
[self animateRemovingDrops:dropsToRemove];
}
return NO;
}
- (void)animateRemovingDrops:(NSArray *)dropsToRemove
{
[UIView animateWithDuration:1.0
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 = self.gameView.bounds.size.height;
drop.center = CGPointMake(x, -y);
}
}
completion:^(BOOL finished) {
[dropsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
}
- (DropitBehavior *)dropitBehaviour
{
if (!_dropitBehaviour) {
_dropitBehaviour = [[DropitBehavior alloc] init];
[self.animator addBehavior:_dropitBehaviour];
}
return _dropitBehaviour;
}
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[self drop];
}
- (IBAction)pan:(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.droppingView) {
self.attachment = [[UIAttachmentBehavior alloc] initWithItem:self.droppingView attachedToAnchor:anchorPoint];
UIView *droppingView = self.droppingView;
__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.droppingView = nil;
[self.animator addBehavior:self.attachment];
}
}
- (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.droppingView = dropView;
[self.dropitBehaviour addItem:dropView];
}
- (UIColor *)randomColor
{
switch (arc4random()%5) {
case 0:
return [UIColor greenColor];
break;
case 1:
return [UIColor blueColor];
break;
case 2:
return [UIColor orangeColor];
break;
case 3:
return [UIColor redColor];
break;
case 4:
return [UIColor purpleColor];
break;
default:
break;
}
return [UIColor blackColor];
}
- (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.
}
@end