2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

  1. //
  2. // SCLongPressGestureRecognizer.m
  3. // SCCamera
  4. //
  5. // Created by Pavlo Antonenko on 4/28/15.
  6. // Copyright (c) 2015 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCLongPressGestureRecognizer.h"
  9. #import <SCFoundation/SCLog.h>
  10. #import <UIKit/UIGestureRecognizerSubclass.h>
  11. @implementation SCLongPressGestureRecognizer {
  12. CGPoint _initialPoint;
  13. CGFloat _initialTime;
  14. }
  15. - (instancetype)initWithTarget:(id)target action:(SEL)action
  16. {
  17. self = [super initWithTarget:target action:action];
  18. if (self) {
  19. _allowableMovementAfterBegan = FLT_MAX;
  20. _timeBeforeUnlimitedMovementAllowed = 0.0;
  21. }
  22. return self;
  23. }
  24. - (void)reset
  25. {
  26. [super reset];
  27. _initialPoint = CGPointZero;
  28. _initialTime = 0;
  29. _forceOfAllTouches = 1.0;
  30. _maximumPossibleForceOfAllTouches = 1.0;
  31. self.failedByMovement = NO;
  32. }
  33. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  34. {
  35. [super touchesBegan:touches withEvent:event];
  36. _initialPoint = [self locationInView:self.view];
  37. _initialTime = CACurrentMediaTime();
  38. _forceOfAllTouches = 1.0;
  39. for (UITouch *touch in touches) {
  40. _maximumPossibleForceOfAllTouches = MAX(touch.maximumPossibleForce, _maximumPossibleForceOfAllTouches);
  41. _forceOfAllTouches = MAX(touch.force, _forceOfAllTouches);
  42. }
  43. }
  44. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  45. {
  46. [super touchesMoved:touches withEvent:event];
  47. _forceOfAllTouches = 1.0;
  48. for (UITouch *touch in touches) {
  49. _maximumPossibleForceOfAllTouches = MAX(touch.maximumPossibleForce, _maximumPossibleForceOfAllTouches);
  50. _forceOfAllTouches = MAX(touch.force, _forceOfAllTouches);
  51. }
  52. if (!CGPointEqualToPoint(_initialPoint, CGPointZero)) {
  53. CGPoint currentPoint = [self locationInView:self.view];
  54. CGFloat distance = hypot(_initialPoint.x - currentPoint.x, _initialPoint.y - currentPoint.y);
  55. CGFloat timeDifference = CACurrentMediaTime() - _initialTime;
  56. if (distance > self.allowableMovementAfterBegan && timeDifference < self.timeBeforeUnlimitedMovementAllowed) {
  57. SCLogGeneralInfo(@"Long press moved %.2f > %.2f after %.3f < %.3f seconds, and is cancelled", distance,
  58. self.allowableMovementAfterBegan, timeDifference, self.timeBeforeUnlimitedMovementAllowed);
  59. self.state = UIGestureRecognizerStateFailed;
  60. self.failedByMovement = YES;
  61. }
  62. }
  63. }
  64. - (void)setEnabled:(BOOL)enabled
  65. {
  66. SCLogGeneralInfo(@"Setting enabled: %d for %@", enabled, self);
  67. [super setEnabled:enabled];
  68. }
  69. - (BOOL)isUnlimitedMovementAllowed
  70. {
  71. return CACurrentMediaTime() - _initialTime > self.timeBeforeUnlimitedMovementAllowed;
  72. }
  73. @end