Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Configure the press and hold gesture recognizer
- touchAndHoldRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(touchAndHold:)];
- touchAndHoldRecognizer.minimumPressDuration = 0.1;
- touchAndHoldRecognizer.allowableMovement = 600;
- [self.view addGestureRecognizer:touchAndHoldRecognizer];
- @interface ViewController ()
- @property (nonatomic) CGPoint location;
- @property (nonatomic, strong) NSTimer *timer;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
- gesture.minimumPressDuration = 0.1;
- gesture.allowableMovement = 600;
- [self.view addGestureRecognizer:gesture];
- }
- - (void)handleTimer:(NSTimer *)timer
- {
- [self someMethod:self.location];
- }
- - (void)handleGesture:(UIGestureRecognizer *)gesture
- {
- self.location = [gesture locationInView:self.view];
- if (gesture.state == UIGestureRecognizerStateBegan)
- {
- self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
- }
- else if (gesture.state == UIGestureRecognizerStateCancelled ||
- gesture.state == UIGestureRecognizerStateFailed ||
- gesture.state == UIGestureRecognizerStateEnded)
- {
- [self.timer invalidate];
- self.timer = nil;
- }
- [self someMethod:self.location];
- }
- - (void)someMethod:(CGPoint)location
- {
- // move whatever you wanted to do in the gesture handler here.
- NSLog(@"%s", __FUNCTION__);
- }
- @end
- [self.scrollview setScrollEnabled:YES];
- /*
- 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.
- 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
- 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.
- the methods below are called by the scroll view and give subclasses override points to add in custom behaviour.
- you can remove the delay in delivery of touchesBegan:withEvent: to subviews by setting delaysContentTouches to NO.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement