即使没有实施,uilongpressgesturerecognizer也会崩溃

我尝试了每一个可能的search,但在一个星期内我没有发现任何类似的东西 我正在制作一个显示表格视图的应用程序。 可以使用单元类中的UIPanGestureRecognizer拖动单元(由自定义类和Interface Builder创build)。 一切正常,除了当我保持手指按在单元格上的应用程序崩溃的错误:

-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0' 

我知道它是什么意思(通常是一个错误的参数发送到实例),但我真的不知道这是如何可能的,因为我的代码中没有UILongPressGestureRecognizer跟踪。 我试图截取LongPressGestureREcognizer EVERYWHERE(在单元格类,在tableview的类,在分配每个单元格之前),但错误仍然是相同的(我看了很多主题在这里的主题和相信我,语法是正确的。

如果你想要任何其他文件随意问(这是我在这里的第一篇文章,我不知道我必须显示)。

谢谢你的宝贵帮助。

好吧,问题仍然有麻烦:我设法设置一个断点并打印堆栈跟踪,这里是:

 0 uBellow 0x0000ac57 -[OpinionCell gestureRecognizerShouldBegin:] + 71 1 UIKit 0x00914939 -[UIGestureRecognizer _shouldBegin] + 1334 2 UIKit 0x0091181a -[UIGestureRecognizer setState:] + 152 3 UIKit 0x00921cea -[UILongPressGestureRecognizer enoughTimeElapsed:] + 127 4 libobjc.A.dylib 0x017186b0 -[NSObject performSelector:withObject:] + 70 5 UIKit 0x00787954 -[UIDelayedAction timerFired:] + 83 6 Foundation 0x0114d2c0 __NSFireTimer + 97 7 CoreFoundation 0x01bce376 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 8 CoreFoundation 0x01bcde06 __CFRunLoopDoTimer + 534 9 CoreFoundation 0x01bb5a82 __CFRunLoopRun + 1810 10 CoreFoundation 0x01bb4f44 CFRunLoopRunSpecific + 276 11 CoreFoundation 0x01bb4e1b CFRunLoopRunInMode + 123 12 GraphicsServices 0x022337e3 GSEventRunModal + 88 13 GraphicsServices 0x02233668 GSEventRun + 104 14 UIKit 0x00648ffc UIApplicationMain + 1211 15 uBellow 0x0000287d main + 141 16 uBellow 0x000027a5 start + 53 

代码上的应用程序块:

 -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint translation = [gestureRecognizer translationInView:[self superview]]; 

编辑我解决了通过截取UILongPressGestureRecognizer,这是一个安全的方式?

 -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){ return NO; }else{ CGPoint translation = [gestureRecognizer translationInView:[self superview]]; // Check for horizontal gesture if (fabsf(translation.x) > fabsf(translation.y)){ return YES; } } return NO;} 

在你的:

 -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer 

尝试改变:

  CGPoint translation = [gestureRecognizer translationInView:[self superview]]; 

至:

  CGPoint translation = [gestureRecognizer locationInView:[self superview]]; 

希望这可以帮助。

我通过检查您收到的gestureRecognizer的types来解决这个问题。

有时我收到了“ UILongPressGestureRecognizer ”而不是“ UIPanGestureRecognizer ”。

尝试这个:

 -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { if ([gestureRecognizer class] == [UIPanGestureRecognizer class]) { CGPoint translation = [gestureRecognizer translationInView:[self superview]]; return YES; } return NO; } 

更新

Swift 2.0版本

 override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool { guard let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else { return false } let translation = panGestureRecognizer.translationInView(self.superview) }