forked from millenomi/muikit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL0DraggableView.m
487 lines (360 loc) · 13.3 KB
/
L0DraggableView.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//
// L0DraggableView.m
// Shard
//
// Created by ∞ on 21/03/09.
// Copyright 2009 Emanuele Vulcano. All rights reserved.
//
#if TARGET_OS_IPHONE
#import "L0DraggableView.h"
static inline BOOL L0VectorHasPointWithinAbsolute(CGPoint vector, CGFloat rangeAbs) {
return
ABS(vector.x) < rangeAbs && ABS(vector.y) < rangeAbs;
}
static inline CGFloat L0ClampToMaximumAbsolute(CGFloat value, CGFloat maximumAbs) {
maximumAbs = ABS(maximumAbs);
if (value > maximumAbs)
value = maximumAbs;
else if (value < -maximumAbs)
value = -maximumAbs;
return value;
}
static inline CGFloat L0ClampToMinimumAbsolute(CGFloat value, CGFloat maximumAbs) {
maximumAbs = ABS(maximumAbs);
if (value < maximumAbs && value > 0)
value = maximumAbs;
else if (value > -maximumAbs && value < 0)
value = -maximumAbs;
return value;
}
#pragma mark -
#pragma mark L0DraggableView private methods
@interface L0DraggableView ()
- (void) _beginDraggingWithTouch:(UITouch*) t;
- (void) _moveByDraggingWithTouch:(UITouch*) t;
- (void) _endDraggingWithTouch:(UITouch*) t;
- (void) _beginPressingWithTouch:(UITouch*) t;
- (BOOL) _tryEndingPressWithTouch:(UITouch*) t;
- (void) _detectPressAndHold;
- (void) _detectPressUp;
- (void) performDraggableViewInitialization;
@end
#pragma mark -
#pragma mark L0DraggableView itself
#define kL0DraggableViewDefaultMaximumSlideDistance 150.0
#define kL0DraggableViewDefaultSpeedDampeningFactor 0.50
@implementation L0DraggableView
- (id) initWithFrame:(CGRect) frame;
{
if (self = [super initWithFrame:frame])
[self performDraggableViewInitialization];
return self;
}
- (id) initWithCoder:(NSCoder*) coder;
{
if (self = [super initWithCoder:coder])
[self performDraggableViewInitialization];
return self;
}
- (void) performDraggableViewInitialization;
{
self.maximumSlideDistances = CGSizeMake(kL0DraggableViewDefaultMaximumSlideDistance, kL0DraggableViewDefaultMaximumSlideDistance);
self.slideSpeedDampeningFactor = kL0DraggableViewDefaultSpeedDampeningFactor;
}
@synthesize maximumSlideDistances, slideSpeedDampeningFactor;
#define kL0DraggableViewPressAndHoldDefaultDelay (0.7)
- (NSTimeInterval) pressAndHoldDelay;
{
if (pressAndHoldDelay <= 0.1) {
L0Log(@"Resetting p&h delay to default %f from %f", kL0DraggableViewPressAndHoldDefaultDelay, pressAndHoldDelay);
pressAndHoldDelay = kL0DraggableViewPressAndHoldDefaultDelay;
}
return pressAndHoldDelay;
}
- (void) setPressAndHoldDelay:(NSTimeInterval) i;
{
pressAndHoldDelay = i;
}
- (void) dealloc;
{
[dragStartDate release];
[super dealloc];
}
#pragma mark -
#pragma mark Event handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
[self _beginPressingWithTouch:[touches anyObject]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch* t = [touches anyObject];
L0Log(@"Touches moved, is pressing? %d, is canceled until touch up? %d", pressingWithoutDrag, draggingCanceledUntilTouchUp);
if (pressingWithoutDrag) {
if (![self _tryEndingPressWithTouch:t]) {
// NO = do not start with drag yet.
return;
}
}
if (!draggingCanceledUntilTouchUp) {
if (!dragging)
[self _beginDraggingWithTouch:t];
else
[self _moveByDraggingWithTouch:t];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
if (pressingWithoutDrag) {
[self _detectPressUp];
return;
}
if (notifyOfPressAndHoldEndOnTouchUp) {
if (delegate && [delegate respondsToSelector:@selector(draggableViewDidEndPressAndHold:)])
[delegate draggableViewDidEndPressAndHold:self];
notifyOfPressAndHoldEndOnTouchUp = NO;
}
if (!draggingCanceledUntilTouchUp)
[self _endDraggingWithTouch:[touches anyObject]];
draggingCanceledUntilTouchUp = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
{
if (!draggingCanceledUntilTouchUp)
[self _endDraggingWithTouch:nil]; // nil == canceled -- no attraction, no slide
if (notifyOfPressAndHoldEndOnTouchUp) {
if (delegate && [delegate respondsToSelector:@selector(draggableViewDidEndPressAndHold:)])
[delegate draggableViewDidEndPressAndHold:self];
notifyOfPressAndHoldEndOnTouchUp = NO;
}
draggingCanceledUntilTouchUp = NO;
}
#pragma mark -
#pragma mark Pressing methods
- (void) _beginPressingWithTouch:(UITouch*) t;
{
if (pressingWithoutDrag || dragging) return;
if (t.tapCount > 1) {
L0Log(@"Tapping multiple times detected: %@", t);
if (delegate && [delegate respondsToSelector:@selector(draggableView:didTapMultipleTimesWithTouch:)])
[delegate draggableView:self didTapMultipleTimesWithTouch:t];
draggingCanceledUntilTouchUp = YES;
return;
} else {
L0Log(@"First touch detected: %@", t);
if (delegate && [delegate respondsToSelector:@selector(draggableView:didTouch:)])
[delegate draggableView:self didTouch:t];
}
L0Log(@"%@, press and hold delay = %f", t, self.pressAndHoldDelay);
pressingWithoutDrag = YES;
pressLocation = [t locationInView:self.superview];
[self performSelector:@selector(_detectPressAndHold) withObject:nil afterDelay:self.pressAndHoldDelay];
}
- (void) _endPressing;
{
L0Log(@"Finished press");
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_detectPressAndHold) object:nil];
pressingWithoutDrag = NO;
}
// YES if dragging should be allowed afterwards, NO otherwise.
- (void) _detectPressAndHold;
{
if (!pressingWithoutDrag || dragging) return;
draggingCanceledUntilTouchUp = NO;
if (delegate && [delegate respondsToSelector:@selector(draggableViewShouldBeginDraggingAfterPressAndHold:)]) {
draggingCanceledUntilTouchUp = ![delegate draggableViewShouldBeginDraggingAfterPressAndHold:self];
}
notifyOfPressAndHoldEndOnTouchUp = YES;
L0Log(@"Detected press and hold -- will cancel dragging until touch up: %d", draggingCanceledUntilTouchUp);
[self _endPressing];
}
- (void) _detectPressUp;
{
if (!pressingWithoutDrag || dragging) return;
L0Log(@"Detected press up");
if (delegate && [delegate respondsToSelector:@selector(draggableViewDidPress:)]) {
[delegate draggableViewDidPress:self];
}
// TODO warn delegate of press up
[self _endPressing];
}
#define kL0DraggableViewPressTolerance 10.0
// if YES, pressing has ended because the user started dragging instead.
- (BOOL) _tryEndingPressWithTouch:(UITouch*) t;
{
CGPoint here = [t locationInView:self.superview];
here.x -= pressLocation.x;
here.y -= pressLocation.y;
L0Log(@"Will check with delta: %@", NSStringFromCGPoint(here));
if (!L0VectorHasPointWithinAbsolute(here, kL0DraggableViewPressTolerance)) {
[self _endPressing];
return YES;
} else
return NO;
}
#pragma mark -
#pragma mark Dragging methods
- (void) _beginDraggingWithTouch:(UITouch*) t;
{
if (dragging) return;
L0Log(@"%@", t);
if (delegate && [delegate respondsToSelector:@selector(draggableViewShouldBeginDragging:)]) {
BOOL go = [delegate draggableViewShouldBeginDragging:self];
if (!go) return;
}
dragging = YES;
lastLocation = [t locationInView:self.superview];
lastSpeedRecordingLocation = lastLocation;
dragStartDate = [NSDate new];
lastSpeedRecordingIntervalSinceStartOfDrag = 0;
self.center = self.center; // stops animation.
if (delegate && [delegate respondsToSelector:@selector(draggableViewDidBeginDragging:)])
[delegate draggableViewDidBeginDragging:self];
[self performSelector:@selector(_recordSpeed) withObject:nil afterDelay:0.05];
}
- (void) _recordSpeed;
{
if (!dragging) return;
lastSpeedRecordingLocation = lastLocation;
lastSpeedRecordingIntervalSinceStartOfDrag = -[dragStartDate timeIntervalSinceNow];
L0Log(@"speed = %@, interval since start = %f", NSStringFromCGPoint(lastSpeedRecordingLocation), lastSpeedRecordingIntervalSinceStartOfDrag);
[self performSelector:@selector(_recordSpeed) withObject:nil afterDelay:0.2];
}
- (void) _moveByDraggingWithTouch:(UITouch*) t;
{
if (!dragging) return;
L0Log(@"%@", t);
CGPoint newLocation = [t locationInView:self.superview];
CGFloat deltaX = newLocation.x - lastLocation.x;
CGFloat deltaY = newLocation.y - lastLocation.y;
CGPoint center = self.center;
center.x += deltaX;
center.y += deltaY;
self.center = center;
if (delegate && delegateImplementsDidDragToPoint)
[delegate draggableView:self didDragToPoint:center];
lastLocation = newLocation;
}
#define kL0DraggableViewMinimumMovementSpeedIn100MSForSlide 7.0
// If t == nil, we stop where we are and never continue with a slide or an attraction.
- (void) _endDraggingWithTouch:(UITouch*) t;
{
if (!dragging) return;
L0Log(@"%@", t);
dragging = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_recordSpeed) object:nil];
NSTimeInterval movementTime = (-[dragStartDate timeIntervalSinceNow]) - lastSpeedRecordingIntervalSinceStartOfDrag;
[dragStartDate release];
dragStartDate = nil;
BOOL continuesWithSlide = NO;
CGPoint movementVector;
if (t) {
NSAssert(self.superview != nil, @"No events should be received without a superview.");
CGPoint here = [t locationInView:self.superview];
movementVector.x = here.x - lastSpeedRecordingLocation.x;
movementVector.y = here.y - lastSpeedRecordingLocation.y;
CGPoint speedPointsPer100MS;
speedPointsPer100MS.x = (movementVector.x / movementTime) * 0.1;
speedPointsPer100MS.y = (movementVector.y / movementTime) * 0.1;
continuesWithSlide = !L0VectorHasPointWithinAbsolute(speedPointsPer100MS, kL0DraggableViewMinimumMovementSpeedIn100MSForSlide);
}
if (delegate && [delegate respondsToSelector:@selector(draggableViewDidEndDragging:continuesWithSlide:)])
[delegate draggableViewDidEndDragging:self continuesWithSlide:continuesWithSlide];
if (!continuesWithSlide) {
// Determine attraction.
// If t == nil, then we have been canceled and we never perform attraction
// in this case.
if (!t && delegate && [delegate respondsToSelector:@selector(draggableView:shouldMoveFromPoint:toAttractionPoint:)]) {
CGPoint to;
BOOL shouldMove = [delegate draggableView:self shouldMoveFromPoint:self.center toAttractionPoint:&to];
if (shouldMove) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(_attractionAnimation:didEndByFinishing:context:)];
[UIView setAnimationDuration:1.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.center = to;
[UIView commitAnimations];
}
}
// No slide? Return.
return;
}
// ===========
// == SLIDE ==
// ===========
CGSize maxSlideDistances = self.maximumSlideDistances;
CGFloat dampening = self.slideSpeedDampeningFactor;
CGFloat startingSlideDistance = sqrt(pow(movementVector.x, 2) + pow(movementVector.y, 2));
if (startingSlideDistance < 30) {
// nx = k*x, ny = k*y
// 30 = sqrt(nx^2 + ny^2)
// 900 = nx^2 + ny^2
// 900 = k^2 * x^2 + k^2 * y^2
// 900 = 2k^2 * (x^2 + y^2)
// 2k^2 = (x^2 + y^2) / 900
// k^2 = (x^2 + y^2) / (900 * 2)
// k = sqrt((x^2 + y^2) / (900 * 2))
CGFloat k = sqrt((pow(movementVector.x, 2) + pow(movementVector.y, 2)) / (900 * 2));
movementVector.x *= k;
movementVector.y *= k;
}
CGPoint delta = movementVector;
int timeScale = 1;
while (!L0VectorHasPointWithinAbsolute(movementVector, 5.0)) {
movementVector.x *= dampening;
movementVector.y *= dampening;
delta.x += movementVector.x;
delta.y += movementVector.y;
timeScale++;
if (ABS(delta.x) > maxSlideDistances.width ||
ABS(delta.y) > maxSlideDistances.height)
break;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementTime * timeScale];
if (delegate) {
L0Log(@"delegate is %@, so setting up animation delegate/stop selector for slide.", delegate);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(_slideAnimation:didEndByFinishing:context:)];
}
L0LogDebugIf(!delegate, @"no delegate set");
CGPoint center = self.center;
center.x += delta.x;
center.y += delta.y;
// ~~~~~~~~~~~~~~~
if (delegate && [delegate respondsToSelector:@selector(draggableView:willBeginInertialSlideToPoint:)])
[delegate draggableView:self willBeginInertialSlideToPoint:center];
if (delegate && [delegate respondsToSelector:@selector(draggableView:shouldMoveFromPoint:toAttractionPoint:)]) {
CGPoint to;
BOOL shouldMove = [delegate draggableView:self shouldMoveFromPoint:center toAttractionPoint:&to];
if (shouldMove) {
// TODO two-part "curve" animation for long attractions
center = to;
}
}
self.center = center;
[UIView commitAnimations];
}
- (void) _slideAnimation:(NSString*) name didEndByFinishing:(BOOL) finished context:(void*) nothing;
{
L0Log(@"finished? = %d", finished);
if (delegate && [delegate respondsToSelector:@selector(draggableView:didEndInertialSlideByFinishing:)])
[delegate draggableView:self didEndInertialSlideByFinishing:finished];
}
- (void) _attractionAnimation:(NSString*) name didEndByFinishing:(BOOL) finished context:(void*) nothing;
{
L0Log(@"finished? = %d", finished);
if (delegate && [delegate respondsToSelector:@selector(draggableView:didEndAttractionByFinishing:)])
[delegate draggableView:self didEndAttractionByFinishing:finished];
}
@synthesize delegate;
- (void) setDelegate:(id <L0DraggableViewDelegate>) d;
{
delegate = d;
delegateImplementsDidDragToPoint = [d respondsToSelector:@selector(draggableView:didDragToPoint:)];
}
@end
#endif