PressGestureRecognizer and touchBegan

I have the following problem. I am using UILongPressGestureRecognizer

to turn UIView into toggle mode. If UIView

in toggle mode, the user can drag the UIView around the screen. To drag the UIView around the screen, I use the touchesBegan

, touchesMoved

and touchesEnded

.

It works, but: I need to lift my finger to drag it because the method touchesBegan

has already been called and therefore not called again and therefore I cannot drag UIView

across the screen.

Is there a way to manually call touchesBegan

after triggering UILongPressGestureRecognizer

( UILongPressGestureRecognizer

changes the BOOL value, but touchesBegan

only works if this BOOL is set to YES).

+3


source to share


2 answers


UILongPressGestureRecognizer

is a continuous gesture recognizer, so instead of resorting to touchesMoved

or UIPanGestureRecognizer

, just check UIGestureRecognizerStateChanged

for example:



- (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"
    }
}

      

+9


source


I suggest you use UIPanGestureRecognizer as the recommended drag and drop gesture.



  • You can adjust the min. and max. the number of touches required to pan using the following properties:

    maximumNumberOfTouches

    minimumNumberOfTouches

  • You can handle states such as Start, Modified, and Completed, for example, have animation for the required states.

  • Using the method below cast the point in UIView where you want.

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

    Example:

    • To keep the old frame you need to use a global variable. Get this in UIGestureRecognizerStateBegan.

    • When the state is UIGestureRecognizerStateChanged. you can use

    -(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.
      }
     }
    
          

  • Drag speed. Based on speed, you can provide an animation showing the bounce of the UIView

    - (CGPoint)velocityInView:(UIView *)view

0


source







All Articles