OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/chrome/browser/ui/side_swipe_gesture_recognizer.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace { | |
12 | |
13 // The absolute maximum swipe angle from |x = y| for a swipe to begin. | |
14 CGFloat MAX_SWIPE_Y_ANGLE = 65; | |
sdefresne
2015/04/02 17:20:29
style: const CGFloat kMaxSwipeYAngleDegree = 65;
| |
15 // The distance between touches for a swipe to begin. | |
16 CGFloat MIN_SWIPE_X_THRESHOLD = 4; | |
sdefresne
2015/04/02 17:20:29
style: const CGFloat kMinSwipeXThresholdPoint = 4;
| |
17 | |
18 } // namespace | |
19 | |
20 @implementation SideSwipeGestureRecognizer | |
21 | |
22 @synthesize swipeEdge = swipeEdge_; | |
23 @synthesize direction = direction_; | |
24 @synthesize swipeOffset = swipeOffset_; | |
25 | |
26 // To quickly avoid interference with other gesture recognizers, fail | |
27 // immediately if the touches aren't at the edge of the touched view. | |
28 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { | |
29 [super touchesBegan:touches withEvent:event]; | |
30 UITouch* touch = [[event allTouches] anyObject]; | |
31 CGPoint location = [touch locationInView:self.view]; | |
32 if (location.x > swipeEdge_ && | |
33 location.x < CGRectGetMaxX([self.view bounds]) - swipeEdge_) { | |
34 self.state = UIGestureRecognizerStateFailed; | |
35 } else { | |
36 if (location.x < swipeEdge_) { | |
37 direction_ = UISwipeGestureRecognizerDirectionRight; | |
38 } else { | |
39 direction_ = UISwipeGestureRecognizerDirectionLeft; | |
40 } | |
41 startPoint_ = location; | |
42 } | |
43 } | |
44 | |
45 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { | |
46 // Revert to normal pan gesture recognizer characteristics after state began. | |
47 if (self.state != UIGestureRecognizerStatePossible) { | |
48 [super touchesMoved:touches withEvent:event]; | |
49 return; | |
50 } | |
51 | |
52 // Only one touch. | |
53 if ([[event allTouches] count] > 1) { | |
54 self.state = UIGestureRecognizerStateFailed; | |
55 return; | |
56 } | |
57 | |
58 // Don't swipe at an angle greater than |MAX_SWIPE_Y_ANGLE|. | |
59 UITouch* touch = [[event allTouches] anyObject]; | |
60 CGPoint currentPoint = [touch locationInView:self.view]; | |
61 CGFloat dy = currentPoint.y - startPoint_.y; | |
62 CGFloat dx = std::abs(currentPoint.x - startPoint_.x); | |
63 CGFloat degrees = fabsf(atan2f(dy, dx) * 180 / float(M_PI)); | |
sdefresne
2015/04/02 17:20:29
nit: std::fabs(std::atan2(dy, dx) * 180 / CGFloat(
| |
64 if (degrees > MAX_SWIPE_Y_ANGLE) { | |
65 self.state = UIGestureRecognizerStateFailed; | |
66 return; | |
67 } | |
68 | |
69 // Don't recognize swipe in the wrong direction. | |
70 if ((direction_ == UISwipeGestureRecognizerDirectionRight && | |
71 currentPoint.x - startPoint_.x < 0) || | |
72 (direction_ == UISwipeGestureRecognizerDirectionLeft && | |
73 currentPoint.x - startPoint_.x > 0)) { | |
74 self.state = UIGestureRecognizerStateFailed; | |
75 return; | |
76 } | |
77 | |
78 // Begin recognizer after |MIN_SWIPE_X_THRESHOLD| distance swiped. | |
79 if (ABS(currentPoint.x - startPoint_.x) > MIN_SWIPE_X_THRESHOLD) { | |
sdefresne
2015/04/02 17:20:29
nit: std::abs(currentPoint.x - startPoint.x)
| |
80 if (direction_ == UISwipeGestureRecognizerDirectionRight) { | |
81 swipeOffset_ = currentPoint.x; | |
82 } else { | |
83 swipeOffset_ = -(CGRectGetMaxX([self.view bounds]) - currentPoint.x); | |
84 } | |
85 | |
86 self.state = UIGestureRecognizerStateBegan; | |
87 return; | |
88 } | |
89 } | |
90 | |
91 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { | |
92 startPoint_ = CGPointZero; | |
93 [super touchesEnded:touches withEvent:event]; | |
94 } | |
95 | |
96 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { | |
97 startPoint_ = CGPointZero; | |
98 [super touchesCancelled:touches withEvent:event]; | |
99 } | |
100 | |
101 @end | |
OLD | NEW |