Ios UILongPressGestureRecognitor即使未实现也会崩溃

Ios UILongPressGestureRecognitor即使未实现也会崩溃,ios,objective-c,uitableview,uigesturerecognizer,Ios,Objective C,Uitableview,Uigesturerecognizer,我尝试了所有可能的搜索,但在一周内没有找到任何类似的。 我正在制作一个显示表视图的应用程序。单元格(由自定义类和Interface Builder创建)可以使用UIPangEstureRecognitor拖动,UIPangEstureRecognitor是单元格类中的pu。 一切正常,只是当我将手指按在手机上时,应用程序会因错误而崩溃: -[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent t

我尝试了所有可能的搜索,但在一周内没有找到任何类似的。 我正在制作一个显示表视图的应用程序。单元格(由自定义类和Interface Builder创建)可以使用UIPangEstureRecognitor拖动,UIPangEstureRecognitor是单元格类中的pu。 一切正常,只是当我将手指按在手机上时,应用程序会因错误而崩溃:

-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x94670a0'
我知道这意味着什么(通常是发送到实例的错误参数),但我真的不知道这怎么可能,因为我的代码中没有UILongPressGestureRecognitor的痕迹。 我尝试在任何地方拦截LongPressGestureRecognitor(在单元格类中,在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]];
编辑 我通过拦截UILongPressGestureRecognitor解决了这个问题,这是一种安全的方法吗

-(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]];
致:

希望这有帮助。

通过检查您收到的手势识别器的类型,我解决了这个问题。

有时我会收到一个“
uilongpressurerecognizer
”而不是“
uipangesturecognizer
”。

试试这个:

-(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)
}

显示
xib
中的对象和任何相关代码单元格对象的图像如下:代码的问题是我没有对UILongPress手势识别器进行任何编码,因此我看不出有什么异常。您的问题是使用默认情况下已集成到UITableView中的自定义手势识别器:(阅读“重新排列表格行”一节如果我看起来很愚蠢,我很抱歉,但这意味着什么?我想使用的唯一手势识别器是PangestureRecognitor(在单元格类中调用和管理)这很好。我不想使用UILongPressGestureRecognitor,所以我没有实现它,但是当我长时间按一个单元格时,应用程序会因为这个错误而崩溃。然后触发异常。然后复制引发异常的堆栈跟踪,并将其粘贴到你的帖子中。你这个天才!非常感谢,它工作得很好!一开始ng我无法滚动回单元格的原始位置,但我将[self superview]更改为self,现在它工作正常:)编辑:(它可以工作,但现在我只能刷前两个单元格,而其他单元格则不行。单元格的高度不同,我正在调查不同大小是否会导致此错误发生……在我的情况下,此解决方案对我也更有效。非常好-知道为什么这可能是一个问题吗?
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    guard let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else { return false }

    let translation = panGestureRecognizer.translationInView(self.superview)
}