我在网络上看到很多关于它的问题,特别是在 StackOverflow 上。 我测试了许多给定的答案,但就我而言,没有任何效果。
我的类实现了协议(protocol)UIGestureRecognizerDelegate
:
@interface CDMapViewController : CDViewController <UIGestureRecognizerDelegate>
以下方法是从我的类的 @implentation 中的 xcode 自动完成编写的
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
NSLog(@"not called");
return NO;
}
我已经在第一个方法中正确初始化了 UIGestureRecognizer,并正确调用了第二个、第三个和第四个方法:
- (void)initGestureOnMap {
UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] init];
gestureRecognizer.delegate = self;
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
gesture_dragging = NO;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
gesture_dragging = YES;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
if (gesture_dragging || [touches count] != 1) return;
/* bla bla bla */
}
...它不记录 - 未调用...为什么?
最佳答案
您需要调用 touches
方法的 super
实现。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
gesture_dragging = NO;
}
... and so on.
这些方法需要在您的 View 上实现,而不是在您的 View Controller 上。
选择您想要的手势类型。 UIGestureRecognizer
本身并没有做太多事情,所以选择一个像 UITapGestureRecognizer
这样的。接下来,使用指定的初始值设定项实现手势识别器。
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethod:)];
最后,实现myMethod:
。
-(void)myMethod:(UITapGestureRecognizer *)recognizer
{
// Whatever this does.
}
关于ios - shouldReceiveTouch 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947676/