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 kMaxSwipeYAngle = 65; |
| 15 // The distance between touches for a swipe to begin. |
| 16 CGFloat kMinSwipeXThreshold = 4; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 @implementation SideSwipeGestureRecognizer { |
| 21 // Starting point of swipe. |
| 22 CGPoint _startPoint; |
| 23 // Expected direction of the swipe, based on starting point. |
| 24 UISwipeGestureRecognizerDirection _direction; |
| 25 } |
| 26 |
| 27 @synthesize swipeEdge = _swipeEdge; |
| 28 @synthesize direction = _direction; |
| 29 @synthesize swipeOffset = _swipeOffset; |
| 30 |
| 31 // To quickly avoid interference with other gesture recognizers, fail |
| 32 // immediately if the touches aren't at the edge of the touched view. |
| 33 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { |
| 34 [super touchesBegan:touches withEvent:event]; |
| 35 UITouch* touch = [[event allTouches] anyObject]; |
| 36 CGPoint location = [touch locationInView:self.view]; |
| 37 if (location.x > _swipeEdge && |
| 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 } |
| 48 } |
| 49 |
| 50 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { |
| 51 // Revert to normal pan gesture recognizer characteristics after state began. |
| 52 if (self.state != UIGestureRecognizerStatePossible) { |
| 53 [super touchesMoved:touches withEvent:event]; |
| 54 return; |
| 55 } |
| 56 |
| 57 // Only one touch. |
| 58 if ([[event allTouches] count] > 1) { |
| 59 self.state = UIGestureRecognizerStateFailed; |
| 60 return; |
| 61 } |
| 62 |
| 63 // Don't swipe at an angle greater than |kMaxSwipeYAngle|. |
| 64 UITouch* touch = [[event allTouches] anyObject]; |
| 65 CGPoint currentPoint = [touch locationInView:self.view]; |
| 66 CGFloat dy = currentPoint.y - _startPoint.y; |
| 67 CGFloat dx = std::abs(currentPoint.x - _startPoint.x); |
| 68 CGFloat degrees = std::fabs(std::atan2(dy, dx) * 180 / CGFloat(M_PI)); |
| 69 if (degrees > kMaxSwipeYAngle) { |
| 70 self.state = UIGestureRecognizerStateFailed; |
| 71 return; |
| 72 } |
| 73 |
| 74 // Don't recognize swipe in the wrong direction. |
| 75 if ((_direction == UISwipeGestureRecognizerDirectionRight && |
| 76 currentPoint.x - _startPoint.x < 0) || |
| 77 (_direction == UISwipeGestureRecognizerDirectionLeft && |
| 78 currentPoint.x - _startPoint.x > 0)) { |
| 79 self.state = UIGestureRecognizerStateFailed; |
| 80 return; |
| 81 } |
| 82 |
| 83 // Begin recognizer after |kMinSwipeXThreshold| distance swiped. |
| 84 if (std::abs(currentPoint.x - _startPoint.x) > kMinSwipeXThreshold) { |
| 85 if (_direction == UISwipeGestureRecognizerDirectionRight) { |
| 86 _swipeOffset = currentPoint.x; |
| 87 } else { |
| 88 _swipeOffset = -(CGRectGetMaxX([self.view bounds]) - currentPoint.x); |
| 89 } |
| 90 |
| 91 self.state = UIGestureRecognizerStateBegan; |
| 92 return; |
| 93 } |
| 94 } |
| 95 |
| 96 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { |
| 97 _startPoint = CGPointZero; |
| 98 [super touchesEnded:touches withEvent:event]; |
| 99 } |
| 100 |
| 101 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { |
| 102 _startPoint = CGPointZero; |
| 103 [super touchesCancelled:touches withEvent:event]; |
| 104 } |
| 105 |
| 106 @end |
OLD | NEW |