Question

Is there any way to get notified for continuous press on UIView? i.e. I keep pressing UIView with one finger and i want to keep calling particular method during that duration again and again.

I tried UILongPressGestureRecognizer but it just get notify of began, end, moved etc. Same as TouchesBegan.

Cheers

Was it helpful?

Solution 3

What you can do is, use the combine functionality of UILongPressGestureRecognizer and NSTimer.

The following code should meet your requirement.

@property (strong, nonatomic) NSTimer *timer;

- (void) viewDidLoad

{

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]         initWithTarget:self action:@selector(longPress:)];

   [aView addGestureRecognizer: longPress];

}

- (void) longPress:(UILongPressGestureRecognizer*)gesture

{

      if ( gesture.state == UIGestureRecognizerStateBegan ) 

  {

        self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(callMethod) userInfo:nil repeats:YES];

        [self.timer fire];

 }
    if ( gesture.state == UIGestureRecognizerStateEnded )

    {

            [self.timer invalidate];

 }

}

- (void) callMethod

{

   //will be called continuously within certain time interval you have set

}

OTHER TIPS

On TouchesBegan start a timer and perform a selector when you've reached a desired amount of time (long press). On TouchesEnded invalidate the timer to prevent the selector from being performed.

I would also set up an extra flag detecting "fingerReleased": Set fingerReleased = NO on TouchesBegan and fingerReleased = YES on TouchesEnded and put the code you want to execute in a:

if (!fingerReleased) 
{
    // Execute code
}

fire NSTimer in touchesBegan to call a selector that you want and invalidate it in touchesEnded method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top