iOS: disable pan gesture when objects overlap

最后都变了- 提交于 2020-01-03 06:51:23

问题


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];
    }
}

回答1:


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];

)




回答2:


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



来源:https://stackoverflow.com/questions/11667329/ios-disable-pan-gesture-when-objects-overlap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!