forked from steipete/PSPushPopPressView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSPushPopPressView.m
executable file
·592 lines (513 loc) · 26.3 KB
/
PSPushPopPressView.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//
// PSPushPopPressView.m
// PSPushPopPressView
//
// Based on BSSPushPopPressView by Blacksmith Software.
//
#import "PSPushPopPressView.h"
#import <QuartzCore/QuartzCore.h>
#define kPSAnimationDuration 0.35f
#define kPSShadowFadeDuration 0.45f
#define kPSAnimationMoveToOriginalPositionDuration 0.5f
#define kPSFullscreenAnimationBounce 20
#define kPSEmbeddedAnimationBounceMultiplier 0.05f
@interface PSPushPopPressView() {
// internal state variables
UIView *initialSuperview_;
BOOL beingDragged_;
BOOL gesturesEnded_;
BOOL scaleActive_;
}
@property (nonatomic, getter=isBeingDragged) BOOL beingDragged;
@property (nonatomic, getter=isFullscreen) BOOL fullscreen;
- (CGRect)windowBounds;
@end
@implementation PSPushPopPressView
@synthesize pushPopPressViewDelegate;
@synthesize beingDragged = beingDragged_;
@synthesize fullscreen = fullscreen_;
@synthesize initialFrame = initialFrame_;
@synthesize allowSingleTapSwitch = allowSingleTapSwitch_;
@synthesize allowFullscreenInteraction = allowFullscreenInteraction_;
@synthesize ignoreStatusBar = ignoreStatusBar_;
@synthesize keepShadow = keepShadow_;
// adapt frame for fullscreen
- (void)detectOrientation {
if (self.isFullscreen) {
self.frame = [self windowBounds];
}
}
- (id)initWithFrame:(CGRect)frame_ {
if ((self = [super initWithFrame:frame_])) {
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
scaleTransform_ = CGAffineTransformIdentity;
rotateTransform_ = CGAffineTransformIdentity;
panTransform_ = CGAffineTransformIdentity;
initialFrame_ = frame_;
initialIndex_ = 0;
allowSingleTapSwitch_ = YES;
allowFullscreenInteraction_ = YES;
keepShadow_ = NO;
UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchPanRotate:)];
pinchRecognizer.cancelsTouchesInView = NO;
pinchRecognizer.delaysTouchesBegan = NO;
pinchRecognizer.delaysTouchesEnded = NO;
pinchRecognizer.delegate = self;
[self addGestureRecognizer: pinchRecognizer];
UIRotationGestureRecognizer* rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(pinchPanRotate:)];
rotationRecognizer.cancelsTouchesInView = NO;
rotationRecognizer.delaysTouchesBegan = NO;
rotationRecognizer.delaysTouchesEnded = NO;
rotationRecognizer.delegate = self;
[self addGestureRecognizer: rotationRecognizer];
panRecognizer_ = [[UIPanGestureRecognizer alloc] initWithTarget: self action:@selector(pinchPanRotate:)];
panRecognizer_.cancelsTouchesInView = NO;
panRecognizer_.delaysTouchesBegan = NO;
panRecognizer_.delaysTouchesEnded = NO;
panRecognizer_.delegate = self;
panRecognizer_.minimumNumberOfTouches = 2;
panRecognizer_.maximumNumberOfTouches = 2;
[self addGestureRecognizer:panRecognizer_];
tapRecognizer_ = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(tap:)];
tapRecognizer_.delegate = self;
tapRecognizer_.cancelsTouchesInView = NO;
tapRecognizer_.delaysTouchesBegan = NO;
tapRecognizer_.delaysTouchesEnded = NO;
[self addGestureRecognizer:tapRecognizer_];
doubleTouchRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)];
doubleTouchRecognizer.delegate = self;
doubleTouchRecognizer.cancelsTouchesInView = NO;
doubleTouchRecognizer.delaysTouchesBegan = NO;
doubleTouchRecognizer.delaysTouchesEnded = NO;
doubleTouchRecognizer.numberOfTouchesRequired = 2;
doubleTouchRecognizer.minimumPressDuration = 0.f;
[self addGestureRecognizer:doubleTouchRecognizer];
swipeRecognizer_ = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: @selector(swipe:)];
swipeRecognizer_.delegate = self;
swipeRecognizer_.cancelsTouchesInView = NO;
swipeRecognizer_.delaysTouchesBegan = NO;
swipeRecognizer_.delaysTouchesEnded = NO;
swipeRecognizer_.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipeRecognizer_];
self.layer.shadowRadius = 15.0f;
self.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.layer.shadowOpacity = 0.4f;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shadowOpacity = 0.0f;
// manually track rotations and adapt fullscreen
// needed if we rotate within a fullscreen animation and miss the autorotate event
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
- (void)dealloc {
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
pushPopPressViewDelegate = nil;
}
- (void)setInitialFrame:(CGRect)initialFrame {
initialFrame_ = initialFrame;
// if we're not in fullscreen, re-set frame
if (!self.isFullscreen) {
self.frame = initialFrame;
}
}
- (UIView *)rootView {
return self.window.rootViewController.view;
}
- (CGRect)windowBounds {
// completely fullscreen
CGRect windowBounds = [self rootView].bounds;
if (self.ignoreStatusBar) {
windowBounds = [UIScreen mainScreen].bounds;
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
windowBounds.size.width = windowBounds.size.height;
windowBounds.size.height = [UIScreen mainScreen].bounds.size.width;
}
}
return windowBounds;
}
- (CGRect)superviewCorrectedInitialFrame {
UIView *rootView = [self rootView];
CGRect superviewCorrectedInitialFrame = [rootView convertRect:initialFrame_ fromView:initialSuperview_];
return superviewCorrectedInitialFrame;
}
- (BOOL)detachViewToWindow:(BOOL)enable {
BOOL viewChanged = NO;
UIView *rootView = [self rootView];
if (enable && !initialSuperview_) {
initialIndex_ = [self.superview.subviews indexOfObject:self];
initialSuperview_ = self.superview;
CGRect newFrame = [self.superview convertRect:initialFrame_ toView:rootView];
[rootView addSubview:self];
[self setFrame:newFrame];
viewChanged = YES;
}else if(!enable) {
if (initialSuperview_) {
[initialSuperview_ insertSubview:self atIndex:initialIndex_];
viewChanged = YES;
}
[self setFrame:initialFrame_];
initialSuperview_ = nil;
}
return viewChanged;
}
- (void)updateShadowPath {
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}
- (void)applyShadowAnimated:(BOOL)animated {
if (keepShadow_) return;
if(animated) {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
anim.fromValue = [NSNumber numberWithFloat:0.0f];
anim.toValue = [NSNumber numberWithFloat:1.0f];
anim.duration = kPSShadowFadeDuration;
[self.layer addAnimation:anim forKey:@"shadowOpacity"];
}else {
[self.layer removeAnimationForKey:@"shadowOpacity"];
}
[self updateShadowPath];
self.layer.shadowOpacity = 1.0f;
}
- (void)removeShadowAnimated:(BOOL)animated {
if (keepShadow_) return;
// TODO: sometimes animates crazy, shadowOpacity animation losses shadowPath transform on certain conditions
// shadow should also use a "lightSource", maybe it's easier to make a completely custom shadow view.
if (animated) {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
anim.fromValue = [NSNumber numberWithFloat:1.0f];
anim.toValue = [NSNumber numberWithFloat:0.0f];
anim.duration = kPSShadowFadeDuration;
[self.layer addAnimation:anim forKey:@"shadowOpacity"];
}else {
[self.layer removeAnimationForKey:@"shadowOpacity"];
}
self.layer.shadowOpacity = 0.0f;
}
- (void)setBeingDragged:(BOOL)newBeingDragged {
if (newBeingDragged != beingDragged_) {
beingDragged_ = newBeingDragged;
if (beingDragged_) {
[self applyShadowAnimated:YES];
}else {
//BOOL animate = !self.isFullscreen && !fullscreenAnimationActive_;
[self removeShadowAnimated:NO];//TODO: removing this shadow animation fixes the (shadow) problem when coming back from fullscreen, that's a good give-and-take for me. The bool was nice writted but I think something messed up in other parts of the code and the check that creates the bool `animate' are messed up so I fixed that the simple way.
}
}
}
- (void)moveViewToOriginalPositionAnimated:(BOOL)animated bounces:(BOOL)bounces {
CGFloat bounceX = panTransform_.tx * kPSEmbeddedAnimationBounceMultiplier * -1;
CGFloat bounceY = panTransform_.ty * kPSEmbeddedAnimationBounceMultiplier * -1;
// switch coordinates of gestureRecognizer in landscape
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
CGFloat tmp = bounceY;
bounceY = bounceX;
bounceX = tmp;
}
self.fullscreen = NO;
if ([self.pushPopPressViewDelegate respondsToSelector:@selector(pushPopPressViewWillAnimateToOriginalFrame:duration:)]) {
[self.pushPopPressViewDelegate pushPopPressViewWillAnimateToOriginalFrame:self duration:kPSAnimationMoveToOriginalPositionDuration*1.5f];
}
[UIView animateWithDuration:animated ? kPSAnimationMoveToOriginalPositionDuration : 0.f delay: 0.0
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
animations:^{
// always reset transforms
rotateTransform_ = CGAffineTransformIdentity;
panTransform_ = CGAffineTransformIdentity;
scaleTransform_ = CGAffineTransformIdentity;
self.transform = CGAffineTransformIdentity;
CGRect correctedInitialFrame = [self superviewCorrectedInitialFrame];
if (bounces) {
if (abs(bounceX) > 0 || abs(bounceY) > 0) {
CGFloat widthDifference = (self.frame.size.width - correctedInitialFrame.size.width) * 0.05;
CGFloat heightDifference = (self.frame.size.height - correctedInitialFrame.size.height) * 0.05;
CGRect targetFrame = CGRectMake(correctedInitialFrame.origin.x + bounceX + (widthDifference / 2.0), correctedInitialFrame.origin.y + bounceY + (heightDifference / 2.0), correctedInitialFrame.size.width + (widthDifference * -1), correctedInitialFrame.size.height + (heightDifference * -1));
[self setFrame:targetFrame];
}else {
// there's reason behind this madness. shadow freaks out when we come from fullscreen, but not if we had transforms.
fullscreenAnimationActive_ = YES;
CGRect targetFrame = CGRectMake(correctedInitialFrame.origin.x + 3, correctedInitialFrame.origin.y + 3, correctedInitialFrame.size.width - 6, correctedInitialFrame.size.height - 6);
//NSLog(@"targetFrame: %@ (superview: %@; initialSuperview: %@)", NSStringFromCGRect(targetFrame), self.superview, self.initialSuperview);
[self setFrame:targetFrame];
}
}else {
[self setFrame:correctedInitialFrame];
}
}
completion: ^(BOOL finished) {
//NSLog(@"moveViewToOriginalPositionAnimated [complete] finished:%d, bounces:%d", finished, bounces);
fullscreenAnimationActive_ = NO;
if (bounces && finished) {
[UIView animateWithDuration: kPSAnimationMoveToOriginalPositionDuration/2 delay: 0.0
options:UIViewAnimationOptionAllowUserInteraction animations: ^{
CGRect correctedInitialFrame = [self superviewCorrectedInitialFrame];
[self setFrame:correctedInitialFrame];
} completion: ^(BOOL finished) {
if (finished && !self.isBeingDragged) {
[self detachViewToWindow:NO];
}
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidAnimateToOriginalFrame:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidAnimateToOriginalFrame: self];
}
}];
}else {
if (!self.isBeingDragged) {
//[self detachViewToWindow:NO];
}
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidAnimateToOriginalFrame:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidAnimateToOriginalFrame: self];
}
}
}];
}
- (void)moveToFullscreenAnimated:(BOOL)animated bounces:(BOOL)bounces {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewWillAnimateToFullscreenWindowFrame:duration:)]) {
[self.pushPopPressViewDelegate pushPopPressViewWillAnimateToFullscreenWindowFrame: self duration: kPSAnimationDuration];
}
BOOL viewChanged = [self detachViewToWindow:YES];
self.fullscreen = YES;
[UIView animateWithDuration: animated ? kPSAnimationDuration : 0.f delay: 0.0
// view hierarchy change needs some time propagating, don't use UIViewAnimationOptionBeginFromCurrentState when just changed
options:(viewChanged ? 0 : UIViewAnimationOptionBeginFromCurrentState) | UIViewAnimationOptionAllowUserInteraction
animations:^{
scaleTransform_ = CGAffineTransformIdentity;
rotateTransform_ = CGAffineTransformIdentity;
panTransform_ = CGAffineTransformIdentity;
self.transform = CGAffineTransformIdentity;
CGRect windowBounds = [self windowBounds];
if (bounces) {
[self setFrame:CGRectMake(windowBounds.origin.x - kPSFullscreenAnimationBounce, windowBounds.origin.y - kPSFullscreenAnimationBounce, windowBounds.size.width + kPSFullscreenAnimationBounce*2, windowBounds.size.height + kPSFullscreenAnimationBounce*2)];
}else {
[self setFrame:windowBounds];
}
}
completion:^(BOOL finished) {
if (bounces && finished) {
CGRect windowBounds = [self windowBounds];
[self detachViewToWindow:YES];
[UIView animateWithDuration:kPSAnimationDuration delay:0.f options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self setFrame:windowBounds];
} completion:^(BOOL finished) {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidAnimateToFullscreenWindowFrame:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidAnimateToFullscreenWindowFrame: self];
}
anchorPointUpdated = NO;
}];
}else {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidAnimateToFullscreenWindowFrame:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidAnimateToFullscreenWindowFrame: self];
}
anchorPointUpdated = NO;
}
}];
}
- (void)alignViewAnimated:(BOOL)animated bounces:(BOOL)bounces {
if (self.frame.size.width > [self windowBounds].size.width) {
[self moveToFullscreenAnimated:animated bounces:bounces];
} else {
[self moveViewToOriginalPositionAnimated:animated bounces:bounces];
}
}
// disrupt gesture recognizer, which continues to receive touch events even as we set minimumNumberOfTouches to two.
- (void)resetGestureRecognizers {
for(UIGestureRecognizer *aGestRec in [self gestureRecognizers]){
[aGestRec setEnabled:NO];
[aGestRec setEnabled:YES];
}
}
- (void)startedGesture:(UIGestureRecognizer *)gesture {
[self detachViewToWindow:YES];
UIPinchGestureRecognizer *pinch = [gesture isKindOfClass:[UIPinchGestureRecognizer class]] ? (UIPinchGestureRecognizer *)gesture : nil;
gesturesEnded_ = NO;
if (pinch) {
scaleActive_ = YES;
}
}
/*
When one gesture ends, the whole view manipulation is ended. In case the user also started a pinch and the pinch is still active, we wait for the pinch to finish as we want to check for a fast pinch movement to open the view in fullscreen or not. If no pinch is active, we can end the manipulation as soon as the first gesture ended.
*/
- (void)endedGesture:(UIGestureRecognizer *)gesture {
if (gesturesEnded_) return;
UIPinchGestureRecognizer *pinch = [gesture isKindOfClass:[UIPinchGestureRecognizer class]] ? (UIPinchGestureRecognizer *)gesture : nil;
if (scaleActive_ == YES && pinch == nil) return;
gesturesEnded_ = YES;
if (pinch) {
scaleActive_ = NO;
if (pinch.velocity >= 2.0f) {
[self moveToFullscreenAnimated:YES bounces:YES];
} else {
[self alignViewAnimated:YES bounces:YES];
}
} else {
[self alignViewAnimated:YES bounces:YES];
}
}
- (void)modifiedGesture:(UIGestureRecognizer *)gesture {
if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]]) {
UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)gesture;
scaleTransform_ = CGAffineTransformScale(CGAffineTransformIdentity, pinch.scale, pinch.scale);
}
else if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]]) {
UIRotationGestureRecognizer *rotate = (UIRotationGestureRecognizer *)gesture;
rotateTransform_ = CGAffineTransformRotate(CGAffineTransformIdentity, rotate.rotation);
}
else if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture;
CGPoint translation = [pan translationInView: self.superview];
panTransform_ = CGAffineTransformTranslate(CGAffineTransformIdentity, translation.x, translation.y);
}
self.transform = CGAffineTransformConcat(CGAffineTransformConcat(scaleTransform_, rotateTransform_), panTransform_);
}
// scale and rotation transforms are applied relative to the layer's anchor point
// this method moves a gesture recognizer's view's anchor point between the user's fingers
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (!anchorPointUpdated) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
anchorPointUpdated = YES;
}
}
- (void)pinchPanRotate:(UIGestureRecognizer *)gesture {
if (self.isFullscreen && !self.allowFullscreenInteraction)
return;
switch (gesture.state) {
case UIGestureRecognizerStateBegan: {
[self adjustAnchorPointForGestureRecognizer:gesture];
[self startedGesture:gesture];
break;
}
case UIGestureRecognizerStatePossible: {
break;
}
case UIGestureRecognizerStateCancelled: {
[self endedGesture:gesture];
anchorPointUpdated = NO;
break;
}
case UIGestureRecognizerStateFailed: {
anchorPointUpdated = NO;
break;
}
case UIGestureRecognizerStateChanged: {
[self modifiedGesture:gesture];
break;
}
case UIGestureRecognizerStateEnded: {
anchorPointUpdated = NO;
[self endedGesture:gesture];
break;
}
}
}
- (void)doubleTapped:(UITapGestureRecognizer *)gesture {
if (self.isFullscreen && !self.allowFullscreenInteraction)
return;
switch (gesture.state) {
case UIGestureRecognizerStateBegan: {
self.beingDragged = YES;
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidStartManipulation:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidStartManipulation:self];
}
break;
}
case UIGestureRecognizerStatePossible: {
break;
}
case UIGestureRecognizerStateCancelled: {
self.beingDragged = NO;
[self resetGestureRecognizers];
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidFinishManipulation:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidFinishManipulation:self];
}
break;
}
case UIGestureRecognizerStateFailed: {
break;
}
case UIGestureRecognizerStateChanged: {
break;
}
case UIGestureRecognizerStateEnded: {
self.beingDragged = NO;
[self resetGestureRecognizers];
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidFinishManipulation:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidFinishManipulation:self];
}
break;
}
}
}
- (void)tap:(UITapGestureRecognizer *)tap {
if (self.isFullscreen && !self.allowFullscreenInteraction)
return;
if (self.allowSingleTapSwitch) {
if (tap.state == UIGestureRecognizerStateEnded) {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewDidReceiveTap:)]) {
[self.pushPopPressViewDelegate pushPopPressViewDidReceiveTap: self];
}
if (!self.isFullscreen) {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewShouldAllowTapToAnimateToFullscreenWindowFrame:)]) {
if ([self.pushPopPressViewDelegate pushPopPressViewShouldAllowTapToAnimateToFullscreenWindowFrame: self] == NO) return;
}
[self moveToFullscreenWindowAnimated:YES];
} else {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewShouldAllowTapToAnimateToOriginalFrame:)]) {
if ([self.pushPopPressViewDelegate pushPopPressViewShouldAllowTapToAnimateToOriginalFrame: self] == NO) return;
}
[self moveToOriginalFrameAnimated:YES];
}
}
}
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
if (self.isFullscreen && !self.allowFullscreenInteraction)
return;
if (swipe.state == UIGestureRecognizerStateEnded) {
if (self.isFullscreen) {
if ([self.pushPopPressViewDelegate respondsToSelector: @selector(pushPopPressViewShouldAllowTapToAnimateToOriginalFrame:)]) {
if ([self.pushPopPressViewDelegate pushPopPressViewShouldAllowTapToAnimateToOriginalFrame: self] == NO) return;
}
[self moveToOriginalFrameAnimated:YES];
}
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// if in fullscreen and fullscreen interaction is disabled, allow simultaneous recognition
if (self.isFullscreen && !self.allowFullscreenInteraction)
return YES;
// if the gesture recognizers's view isn't one of our pieces, don't allow simultaneous recognition
if (gestureRecognizer.view != self)
return NO;
// if the gesture recognizers are on different views, don't allow simultaneous recognition
if (gestureRecognizer.view != otherGestureRecognizer.view)
return NO;
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [touch.view isKindOfClass: [UIButton class]]) {
return NO;
}
return YES;
}
- (void)moveToFullscreenWindowAnimated:(BOOL)animated {
if (self.isFullscreen) return;
[self moveToFullscreenAnimated:animated bounces:YES];
}
- (void)moveToOriginalFrameAnimated:(BOOL)animated {
if (self.isFullscreen == NO) return;
[self moveViewToOriginalPositionAnimated:animated bounces:YES];
}
// enable/disable single tap detection
- (void)setAllowSingleTapSwitch:(BOOL)allowSingleTapSwitch {
if (allowSingleTapSwitch_ != allowSingleTapSwitch) {
allowSingleTapSwitch_ = allowSingleTapSwitch;
tapRecognizer_.enabled = allowSingleTapSwitch;
}
}
@end