Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. // Configure the press and hold gesture recognizer
  2. touchAndHoldRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(touchAndHold:)];
  3. touchAndHoldRecognizer.minimumPressDuration = 0.1;
  4. touchAndHoldRecognizer.allowableMovement = 600;
  5. [self.view addGestureRecognizer:touchAndHoldRecognizer];
  6.  
  7. @interface ViewController ()
  8.  
  9. @property (nonatomic) CGPoint location;
  10. @property (nonatomic, strong) NSTimer *timer;
  11.  
  12. @end
  13.  
  14. @implementation ViewController
  15.  
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19.  
  20. UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
  21. gesture.minimumPressDuration = 0.1;
  22. gesture.allowableMovement = 600;
  23. [self.view addGestureRecognizer:gesture];
  24. }
  25.  
  26. - (void)handleTimer:(NSTimer *)timer
  27. {
  28. [self someMethod:self.location];
  29. }
  30.  
  31. - (void)handleGesture:(UIGestureRecognizer *)gesture
  32. {
  33. self.location = [gesture locationInView:self.view];
  34.  
  35. if (gesture.state == UIGestureRecognizerStateBegan)
  36. {
  37. self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
  38. }
  39. else if (gesture.state == UIGestureRecognizerStateCancelled ||
  40. gesture.state == UIGestureRecognizerStateFailed ||
  41. gesture.state == UIGestureRecognizerStateEnded)
  42. {
  43. [self.timer invalidate];
  44. self.timer = nil;
  45. }
  46.  
  47. [self someMethod:self.location];
  48. }
  49.  
  50. - (void)someMethod:(CGPoint)location
  51. {
  52. // move whatever you wanted to do in the gesture handler here.
  53.  
  54. NSLog(@"%s", __FUNCTION__);
  55. }
  56.  
  57. @end
  58.  
  59. [self.scrollview setScrollEnabled:YES];
  60.  
  61. /*
  62. Scrolling with no scroll bars is a bit complex. on touch down, we don't know if the user will want to scroll or track a subview like a control.
  63. on touch down, we start a timer and also look at any movement. if the time elapses without sufficient change in position, we start sending events to
  64. the hit view in the content subview. if the user then drags far enough, we switch back to dragging and cancel any tracking in the subview.
  65. the methods below are called by the scroll view and give subclasses override points to add in custom behaviour.
  66. you can remove the delay in delivery of touchesBegan:withEvent: to subviews by setting delaysContentTouches to NO.
  67. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement