问题
I have a few objects moved by using Pan Gesture
. Now I want the object to stop moving permanently when either:
- It overlaps a particular (stationary, not able to be moved) object, or
- 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