-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLMEdgePanGestureRecognizer.m
82 lines (66 loc) · 2.44 KB
/
LMEdgePanGestureRecognizer.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
//
// LMPanGestureRecognizer.m
// Photos
//
// Created by Logan Moseley on 1/21/13.
// Copyright (c) 2013 Logan Moseley. All rights reserved.
//
#import "LMEdgePanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
static CGFloat const kReceivingTouchMarginDefault = 20.;
static LMEdgePanGestureRecognizerEdge const kRecognizedEdgesDefault = LMEdgePanGestureRecognizerEdgeBottom;
@implementation LMEdgePanGestureRecognizer
- (id)initWithTarget:(id)target action:(SEL)action
{
self = [super initWithTarget:target action:action];
if (self) {
self.touchMargin = kReceivingTouchMarginDefault;
self.edge = kRecognizedEdgesDefault;
}
return self;
}
- (BOOL)cancelsTouchesInView
{
return YES;
}
// Returns YES, except when the arg is an EdgePanGestureRecognizer
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
{
BOOL p = ![preventedGestureRecognizer isKindOfClass:[LMEdgePanGestureRecognizer class]];
return p;
}
// Returns NO, except when the arg is an EdgePanGestureRecognizer{
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
BOOL p = [preventingGestureRecognizer isKindOfClass:[self class]];
return p;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([self shouldCaptureTouch:touch]) {
[super touchesBegan:touches withEvent:event];
}
}
- (UIEdgeInsets)insetsForTouchDetection
{
UIEdgeInsets insets = UIEdgeInsetsZero;
insets.top = self.edge & LMEdgePanGestureRecognizerEdgeTop ? self.touchMargin : 0.;
insets.left = self.edge & LMEdgePanGestureRecognizerEdgeLeft ? self.touchMargin : 0.;
insets.bottom = self.edge & LMEdgePanGestureRecognizerEdgeBottom ? self.touchMargin : 0.;
insets.right = self.edge & LMEdgePanGestureRecognizerEdgeRight ? self.touchMargin : 0.;
return insets;
}
- (BOOL)shouldCaptureTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.view];
CGRect viewBounds = self.view.frame;
viewBounds.origin = CGPointZero;
bool viewContainsPoint = CGRectContainsPoint(viewBounds, location);
if (!viewContainsPoint) return NO;
CGRect insetBounds = UIEdgeInsetsInsetRect(viewBounds, [self insetsForTouchDetection]);
bool insetContainsPoint = CGRectContainsPoint(insetBounds, location);
if (insetContainsPoint) return NO;
return YES;
}
@end