Question

I have the following problem. I am using a UILongPressGestureRecognizer to put a UIView into a "toggle mode". If the UIView is in "toggle mode" the user is able to drag the UIView around the screen. For dragging the UIView around the screen I am using the methods touchesBegan, touchesMoved and touchesEnded.

It works, but: I have to lift my finger in order to drag it, because the touchesBegan method got already called and therefore is not called again and therefore I can't drag the UIView around the screen.

Is there any way to manually call touchesBegan after UILongPressGestureRecognizer got triggered (UILongPressGestureRecognizer changes a BOOL value and the touchesBegan only works if this BOOL is set to YES).

Was it helpful?

Solution

UILongPressGestureRecognizer is a continuous gesture recognizer, so rather than resorting to touchesMoved or UIPanGestureRecognizer, just check for UIGestureRecognizerStateChanged, e.g.:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self.view addGestureRecognizer:gesture];
}

- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // user held down their finger on the screen

        // gesture started, entering the "toggle mode"
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // user did not lift finger, but now proceeded to move finger

        // do here whatever you wanted to do in the touchesMoved
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // user lifted their finger

        // all done, leaving the "toggle mode"
    }
}

OTHER TIPS

I would suggest you to use UIPanGestureRecognizer as it a recommended gesture for dragging.

  1. You can configure the min. and max. number of touches required for a panning, using the following the properties:

    maximumNumberOfTouches

    minimumNumberOfTouches

  2. You can handle the states like Began, Changed and Ended, like having animation for the required states.

  3. Using the below method translate the point to the UIView in which you want it.

    - (void)setTranslation:(CGPoint)translation inView:(UIView *)view

    example:

    1. You have to use a global variable to retain the old frame. Get this in UIGestureRecognizerStateBegan.

    2. When the state is UIGestureRecognizerStateChanged. You can use the

    -(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{
    
       if(panGesture.state==UIGestureRecognizerStateBegan){
         //retain the original state
       }else if(panGesture.state==UIGestureRecognizerStateChanged){
       CGPoint translatedPoint=[panGesture translationInView:self.view];
      //here you manage to get your new drag points.
      }
     }
    
  4. Velocity of the drag. Based on the velocity you can provide a animation to show bouncing of a UIView

    - (CGPoint)velocityInView:(UIView *)view

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