YYGestureRecognizer.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // YYGestureRecognizer.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/26.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "YYGestureRecognizer.h"
  12. #import <UIKit/UIGestureRecognizerSubclass.h>
  13. @implementation YYGestureRecognizer
  14. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  15. self.state = UIGestureRecognizerStateBegan;
  16. _startPoint = [(UITouch *)[touches anyObject] locationInView:self.view];
  17. _lastPoint = _currentPoint;
  18. _currentPoint = _startPoint;
  19. if (_action) _action(self, YYGestureRecognizerStateBegan);
  20. }
  21. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  22. UITouch *touch = (UITouch *)[touches anyObject];
  23. CGPoint currentPoint = [touch locationInView:self.view];
  24. self.state = UIGestureRecognizerStateChanged;
  25. _currentPoint = currentPoint;
  26. if (_action) _action(self, YYGestureRecognizerStateMoved);
  27. _lastPoint = _currentPoint;
  28. }
  29. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  30. self.state = UIGestureRecognizerStateEnded;
  31. if (_action) _action(self, YYGestureRecognizerStateEnded);
  32. }
  33. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  34. self.state = UIGestureRecognizerStateCancelled;
  35. if (_action) _action(self, YYGestureRecognizerStateCancelled);
  36. }
  37. - (void)reset {
  38. self.state = UIGestureRecognizerStatePossible;
  39. }
  40. - (void)cancel {
  41. if (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged) {
  42. self.state = UIGestureRecognizerStateCancelled;
  43. if (_action) _action(self, YYGestureRecognizerStateCancelled);
  44. }
  45. }
  46. @end