OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/side_swipe_gesture_recognizer.h" | 5 #import "ios/chrome/browser/ui/side_swipe_gesture_recognizer.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // The absolute maximum swipe angle from |x = y| for a swipe to begin. | 13 // The absolute maximum swipe angle from |x = y| for a swipe to begin. |
14 const CGFloat kMaxSwipeYAngle = 65; | 14 const CGFloat kMaxSwipeYAngle = 65; |
15 // The distance between touches for a swipe to begin. | 15 // The distance between touches for a swipe to begin. |
16 const CGFloat kMinSwipeXThreshold = 4; | 16 const CGFloat kMinSwipeXThreshold = 4; |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 @implementation SideSwipeGestureRecognizer { | 20 @implementation SideSwipeGestureRecognizer { |
21 // Starting point of swipe. | |
22 CGPoint _startPoint; | |
23 // Expected direction of the swipe, based on starting point. | 21 // Expected direction of the swipe, based on starting point. |
24 UISwipeGestureRecognizerDirection _direction; | 22 UISwipeGestureRecognizerDirection _direction; |
25 } | 23 } |
26 | 24 |
27 @synthesize swipeEdge = _swipeEdge; | 25 @synthesize swipeEdge = _swipeEdge; |
28 @synthesize direction = _direction; | 26 @synthesize direction = _direction; |
29 @synthesize swipeOffset = _swipeOffset; | 27 @synthesize swipeOffset = _swipeOffset; |
| 28 @synthesize startPoint = _startPoint; |
30 | 29 |
31 // To quickly avoid interference with other gesture recognizers, fail | 30 // To quickly avoid interference with other gesture recognizers, fail |
32 // immediately if the touches aren't at the edge of the touched view. | 31 // immediately if the touches aren't at the edge of the touched view. |
33 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { | 32 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { |
34 [super touchesBegan:touches withEvent:event]; | 33 [super touchesBegan:touches withEvent:event]; |
35 UITouch* touch = [[event allTouches] anyObject]; | 34 UITouch* touch = [[event allTouches] anyObject]; |
36 CGPoint location = [touch locationInView:self.view]; | 35 CGPoint location = [touch locationInView:self.view]; |
37 if (location.x > _swipeEdge && | 36 if (_swipeEdge > 0) { |
38 location.x < CGRectGetMaxX([self.view bounds]) - _swipeEdge) { | 37 if (location.x > _swipeEdge && |
39 self.state = UIGestureRecognizerStateFailed; | 38 location.x < CGRectGetMaxX([self.view bounds]) - _swipeEdge) { |
| 39 self.state = UIGestureRecognizerStateFailed; |
| 40 } else { |
| 41 if (location.x < _swipeEdge) { |
| 42 _direction = UISwipeGestureRecognizerDirectionRight; |
| 43 } else { |
| 44 _direction = UISwipeGestureRecognizerDirectionLeft; |
| 45 } |
| 46 _startPoint = location; |
| 47 } |
40 } else { | 48 } else { |
41 if (location.x < _swipeEdge) { | |
42 _direction = UISwipeGestureRecognizerDirectionRight; | |
43 } else { | |
44 _direction = UISwipeGestureRecognizerDirectionLeft; | |
45 } | |
46 _startPoint = location; | 49 _startPoint = location; |
| 50 _direction = 0; |
47 } | 51 } |
48 } | 52 } |
49 | 53 |
50 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { | 54 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { |
51 // Revert to normal pan gesture recognizer characteristics after state began. | 55 // Revert to normal pan gesture recognizer characteristics after state began. |
52 if (self.state != UIGestureRecognizerStatePossible) { | 56 if (self.state != UIGestureRecognizerStatePossible) { |
53 [super touchesMoved:touches withEvent:event]; | 57 [super touchesMoved:touches withEvent:event]; |
54 return; | 58 return; |
55 } | 59 } |
56 | 60 |
(...skipping 16 matching lines...) Expand all Loading... |
73 | 77 |
74 // Don't recognize swipe in the wrong direction. | 78 // Don't recognize swipe in the wrong direction. |
75 if ((_direction == UISwipeGestureRecognizerDirectionRight && | 79 if ((_direction == UISwipeGestureRecognizerDirectionRight && |
76 currentPoint.x - _startPoint.x < 0) || | 80 currentPoint.x - _startPoint.x < 0) || |
77 (_direction == UISwipeGestureRecognizerDirectionLeft && | 81 (_direction == UISwipeGestureRecognizerDirectionLeft && |
78 currentPoint.x - _startPoint.x > 0)) { | 82 currentPoint.x - _startPoint.x > 0)) { |
79 self.state = UIGestureRecognizerStateFailed; | 83 self.state = UIGestureRecognizerStateFailed; |
80 return; | 84 return; |
81 } | 85 } |
82 | 86 |
| 87 if (_swipeEdge == 0 && _direction == 0) { |
| 88 if (currentPoint.x > _startPoint.x) { |
| 89 _direction = UISwipeGestureRecognizerDirectionRight; |
| 90 } else { |
| 91 _direction = UISwipeGestureRecognizerDirectionLeft; |
| 92 } |
| 93 } |
| 94 |
83 // Begin recognizer after |kMinSwipeXThreshold| distance swiped. | 95 // Begin recognizer after |kMinSwipeXThreshold| distance swiped. |
84 if (std::abs(currentPoint.x - _startPoint.x) > kMinSwipeXThreshold) { | 96 if (std::abs(currentPoint.x - _startPoint.x) > kMinSwipeXThreshold) { |
85 if (_direction == UISwipeGestureRecognizerDirectionRight) { | 97 if (_direction == UISwipeGestureRecognizerDirectionRight) { |
86 _swipeOffset = currentPoint.x; | 98 _swipeOffset = currentPoint.x; |
87 } else { | 99 } else { |
88 _swipeOffset = -(CGRectGetMaxX([self.view bounds]) - currentPoint.x); | 100 _swipeOffset = -(CGRectGetMaxX([self.view bounds]) - currentPoint.x); |
89 } | 101 } |
90 | 102 |
91 self.state = UIGestureRecognizerStateBegan; | 103 self.state = UIGestureRecognizerStateBegan; |
92 return; | 104 return; |
93 } | 105 } |
94 } | 106 } |
95 | 107 |
96 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { | 108 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { |
97 _startPoint = CGPointZero; | 109 _startPoint = CGPointZero; |
98 [super touchesEnded:touches withEvent:event]; | 110 [super touchesEnded:touches withEvent:event]; |
99 } | 111 } |
100 | 112 |
101 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { | 113 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { |
102 _startPoint = CGPointZero; | 114 _startPoint = CGPointZero; |
103 [super touchesCancelled:touches withEvent:event]; | 115 [super touchesCancelled:touches withEvent:event]; |
104 } | 116 } |
105 | 117 |
106 @end | 118 @end |
OLD | NEW |