我在ViewController的viewDidLoad中有这段代码
UILongPressGestureRecognizer* gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_handleLongPress:)];
gestureRecognizer.numberOfTouchesRequired = 1;
gestureRecognizer.minimumPressDuration = 5.0;
gestureRecognizer.delegate = self;
[self.view addGestureRecognizer:gestureRecognizer];
NSLog(@"Gesture recognizers = %@",self.view.gestureRecognizers);
和方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
- (void) _handleLongPress:(UIGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
NSLog(@"Started");
}
当我运行此应用并点击视图5秒钟时,_handleLongPress
会触发,但是当我在设备上运行相同的应用时,它永远不会点击_handleLongPress
方法。我是什么意思失踪?我确实看到了长按的输出:
2014-08-01 10:00:48.505 TestConsole[1343:60b] Gesture recognizers = (
"UILongPressGestureRecognizer: 0x14644d30; state = Possible; view = UIView 0x1453f950>; target= <(action= _handleLongPress:, target=MyViewController 0x1463a730>)>"
我把断点放在gestureRecognizer:shouldReceiveTouch:
,它确实击中了UILongPressGestureRecognizer ..我傻眼了!