-
Notifications
You must be signed in to change notification settings - Fork 1
/
DraggableView.m
219 lines (177 loc) · 7.07 KB
/
DraggableView.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// DraggableView.m
// TindermChallenge
//
// Created by Mary Jenel Myers on 4/6/27 H.
// Copyright (c) 27 Heisei Mary Jenel Myers. All rights reserved.
//
#define ACTION_MARGIN 120 //%%% distance from center where the action applies. Higher = swipe further in order for the action to be called
#define SCALE_STRENGTH 4 //%%% how quickly the card shrinks. Higher = slower shrinking
#define SCALE_MAX .93 //%%% upper bar for how much the card shrinks. Higher = shrinks less
#define ROTATION_MAX 1 //%%% the maximum rotation allowed in radians. Higher = card can keep rotating longer
#define ROTATION_STRENGTH 320 //%%% strength of rotation. Higher = weaker rotation
#define ROTATION_ANGLE M_PI/8 //%%% Higher = stronger rotation angle
#import "DraggableView.h"
@implementation DraggableView {
CGFloat xFromCenter;
CGFloat yFromCenter;
}
//delegate is instance of ViewController
@synthesize delegate;
@synthesize panGestureRecognizer;
@synthesize overlayView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//grabs an array of views creating from the Nib
//can create multiple views in the XIB file
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"DraggableView" owner:self options:nil];
self = [subviewArray objectAtIndex:0];
self.frame = frame;
[self setupView];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
[self addGestureRecognizer:panGestureRecognizer];
overlayView = [[OverlayView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-100, 0, 100, 100)];
overlayView.alpha = 0;
[self addSubview:overlayView];
}
return self;
}
-(void)setupView
{
self.layer.cornerRadius = 4;
self.layer.shadowRadius = 3;
self.layer.shadowOpacity = 0.2;
self.layer.shadowOffset = CGSizeMake(1, 1);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
//%%% called when you move your finger across the screen.
// called many times a second
-(void)beingDragged:(UIPanGestureRecognizer *)gestureRecognizer
{
//%%% this extracts the coordinate data from your swipe movement. (i.e. How much did you move?)
xFromCenter = [gestureRecognizer translationInView:self].x; //%%% positive for right swipe, negative for left
yFromCenter = [gestureRecognizer translationInView:self].y; //%%% positive for up, negative for down
//%%% checks what state the gesture is in. (are you just starting, letting go, or in the middle of a swipe?)
switch (gestureRecognizer.state) {
//%%% just started swiping
case UIGestureRecognizerStateBegan:{
self.originalPoint = self.center;
break;
};
//%%% in the middle of a swipe
case UIGestureRecognizerStateChanged:{
//%%% dictates rotation (see ROTATION_MAX and ROTATION_STRENGTH for details)
CGFloat rotationStrength = MIN(xFromCenter / ROTATION_STRENGTH, ROTATION_MAX);
//%%% degree change in radians
CGFloat rotationAngel = (CGFloat) (ROTATION_ANGLE * rotationStrength);
//%%% amount the height changes when you move the card up to a certain point
CGFloat scale = MAX(1 - fabsf(rotationStrength) / SCALE_STRENGTH, SCALE_MAX);
//%%% move the object's center by center + gesture coordinate
self.center = CGPointMake(self.originalPoint.x + xFromCenter, self.originalPoint.y + yFromCenter);
//%%% rotate by certain amount
CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
//%%% scale by certain amount
CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
//%%% apply transformations
self.transform = scaleTransform;
[self updateOverlay:xFromCenter];
break;
};
//%%% let go of the card
case UIGestureRecognizerStateEnded: {
[self afterSwipeAction];
break;
};
case UIGestureRecognizerStatePossible:break;
case UIGestureRecognizerStateCancelled:break;
case UIGestureRecognizerStateFailed:break;
}
}
//%%% checks to see if you are moving right or left and applies the correct overlay image
-(void)updateOverlay:(CGFloat)distance
{
if (distance > 0) {
overlayView.mode = GGOverlayViewModeRight;
} else {
overlayView.mode = GGOverlayViewModeLeft;
}
overlayView.alpha = MIN(fabsf(distance)/100, 0.4);
}
// called when the card is let go
- (void)afterSwipeAction
{
if (xFromCenter > ACTION_MARGIN) {
[self rightAction];
} else if (xFromCenter < -ACTION_MARGIN) {
[self leftAction];
} else { //%%% resets the card
[UIView animateWithDuration:0.3
animations:^{
self.center = self.originalPoint;
self.transform = CGAffineTransformMakeRotation(0);
overlayView.alpha = 0;
}];
}
}
//%%% called when a swipe exceeds the ACTION_MARGIN to the right
-(void)rightAction
{
CGPoint finishPoint = CGPointMake(500, 2*yFromCenter +self.originalPoint.y);
[UIView animateWithDuration:0.3
animations:^{
self.center = finishPoint;
}completion:^(BOOL complete){
[self removeFromSuperview];
}];
[delegate cardSwipedRight:self];
NSLog(@"YES");
}
//%%% called when a swip exceeds the ACTION_MARGIN to the left
-(void)leftAction
{
CGPoint finishPoint = CGPointMake(-500, 2*yFromCenter +self.originalPoint.y);
[UIView animateWithDuration:0.3
animations:^{
self.center = finishPoint;
}completion:^(BOOL complete){
[self removeFromSuperview];
}];
[delegate cardSwipedLeft:self];
NSLog(@"NO");
}
-(void)rightClickAction
{
CGPoint finishPoint = CGPointMake(600, self.center.y);
[UIView animateWithDuration:0.3
animations:^{
self.center = finishPoint;
self.transform = CGAffineTransformMakeRotation(1);
}completion:^(BOOL complete){
[self removeFromSuperview];
}];
[delegate cardSwipedRight:self];
NSLog(@"YES");
}
-(void)leftClickAction
{
CGPoint finishPoint = CGPointMake(-600, self.center.y);
[UIView animateWithDuration:0.3
animations:^{
self.center = finishPoint;
self.transform = CGAffineTransformMakeRotation(-1);
}completion:^(BOOL complete){
[self removeFromSuperview];
}];
[delegate cardSwipedLeft:self];
NSLog(@"NO");
}
@end