简体   繁体   中英

iOS: disable pan gesture when objects overlap

I have a few objects moved by using Pan Gesture . Now I want the object to stop moving permanently when either:

  1. It overlaps a particular (stationary, not able to be moved) object, or
  2. It enters a certain range of locations.

I've tried to stop it when the two objects overlap using removeGestureRecogniser but it didn't work.

- (IBAction)cowimagemove:(UIPanGestureRecognizer *)recognizer {

if (cowimage.center.x==stayimage.center.x) {
    [self removeGestureRecogniser];
    }
else {
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    }
}

Try catching the overlap in your UIGestureRecognizer Delegate.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
   if ([self checkForOverlap:gestureRecognizer]) {
       return NO;
   } 
   return YES;

}

In your checkForOverlap method you use the gesture recognizer object to test for the relevant points etc. and return YES if it is the case.

(Of course a shorter version of the above would be

return ![self checkForOverlap:gestureRecognizer];

)

since you did not include any set-up code, this answer is a little like poking around in the dark, but the first thing that hits me is that removeGestureRecognizer is a method that requires a parameter, namely the gesture recognizer you want to remove. So the 3rd line show read

[self removeGestureRecognizer: recognizer];

One reason for always passing these references around in all delegate methods is precisely that - you know which object your working for...

In all cases where I used addGestureRecognizer/removeGestureRecognizer pairs, they worked smoothly, so let's hope they do in your case as well!

Regards, nobi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM