UILongPressGestureRecognizer stops the handle without stopping the touch

I use the UILongPressGestureRecognizer class to handle if one item is selected.

The logic is this: the user clicks an element within 1 second (subclass of UIView). As soon as a gesture is detected, the element is highlighted and moved.

The user must move this item around the screen without touching it.

The problem I am facing is a recognized sign of shadows regarding Began / Move / Ended, necessary for the element class to organize the movement.

I tried to remove the recognized attribute detected once and the selected item. But still sending messages to the gesture descriptor, not the calls, affects the methods.

Does anyone know how to stop "listening" to the gesture recognizer without leaving your finger on the screen?

Thanks.

Here is the code:

-(void)addGestures { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; longPress.minimumPressDuration = iItemLongPressTime; [self addGestureRecognizer:longPress]; [longPress release]; } - (void)handleLongPress:(UILongPressGestureRecognizer*)sender { if (sender.state == UIGestureRecognizerStateEnded) { NSLog(@"Long press Ended"); } else { if (self.isSelected) return; if ([delegate respondsToSelector:@selector(singleTouch:)]) [delegate singleTouch:self]; [self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]]; NSLog(@"Long press detected."); } } 

As you can see in the else branch, delegate calls allow all procedures to mark this element as selected immediately after deleting resolvers.

What am I missing?

- EDIT -

Done! It works:

 #pragma mark Gesture Functions -(void)addGestures { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; longPress.minimumPressDuration = iItemLongPressTime; [self addGestureRecognizer:longPress]; [longPress release]; } - (void)handleLongPress:(UILongPressGestureRecognizer*)sender { if (sender.state == UIGestureRecognizerStateEnded) { NSLog(@"Long press Ended"); } else { NSLog(@"Long press detected."); if (self.isSelected) return; if ([delegate respondsToSelector:@selector(singleTouch:)]) [delegate singleTouch:self]; [sender removeTarget:self action:@selector(handleLongPress:)]; sender.enabled = NO; [self removeGestureRecognizer:sender]; } } 

Hello!

+6
source share
2 answers

Does the custom UIView class have its own touch processing code? If not, a simple solution is to set the allowableMovement property from UILongPressGestureRecognizer to CGFLOAT_MAX , or some large number, and use gesture update callbacks to drag your custom view. You can get the offset using the - (CGPoint)locationInView:(UIView *)view method in supervision, and compare its position with the beginning of the recognizer.

+4
source

There are two solutions in my mind.

  • To animate uiview, please write a new class that inherits from the UIView class and implements touch delegates instead of writing Gustures to handle the animation (if touch delegates do not start in the current class).

2. I successfully uninstalled the UILongPressGestureRecognizer after it started once.

Please refer to the code below. let me in if you have any questions

The steps that I followed

I added UIView as "myView" to my main view when loading the main view.

I gave the myView tag (you can give 1,2,3 ... etc.) to distinguish the displayed view from the subviews of the main view.

Assign a UILongPressGestureRecognizer gesture to myView and assign the target as the moveMe method.

When the user clicks myView long, the moveMe method is launched.

Then I repeated mainview Subviews with the condition Tag == 1

I removed UILongPressGestureRecognizer from subview.As we can know that TagView 1 of the main view of subView is myView.

So, NSLog (gesture "@ deleted"); and NSLog (@ "moveMe"); will enter the console only at a time.

NSLog (@ "touchhesBegan"); it starts, not the moveMe method first.

Then NSLog (@ "touchhesBegan"); will always work after deleting gestures. The moveMe method will never run.

the code

  - (void)viewDidLoad { //Adding to UIView to main view when application is loading. UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)]; myView.backgroundColor = [UIColor viewFlipsideBackgroundColor]; myView.tag = 1; //adding a tag to identify it. //Adding Long Press Gesture to the UIView. UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveMe:)]; [myView addGestureRecognizer:myGesture]; [myGesture release]; myGesture = nil; [self.view addSubview:myView]; [myView release]; myView = nil; [super viewDidLoad]; } //Method to trigger when user pressed long on the added UIView. -(void)moveMe:(id)sender { for (UIView *subViews in [self.view subviews]) { if (subViews.tag == 1) { [subViews removeGestureRecognizer:sender]; NSLog(@"gesture removed"); } } NSLog(@"moveMe"); } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan"); } 

or refer to Disable iOS gesture recognizer

+1
source

Source: https://habr.com/ru/post/910613/


All Articles