4

我尝试了所有可能的搜索,但在一周内我没有找到任何类似的东西。我正在制作一个显示表格视图的应用程序。单元格(由自定义类和界面生成器制作)可以使用单元格类中的 pu 的 UIPanGestureRecognizer 拖动。一切正常,除了当我将手指按在单元格上时,应用程序因错误而崩溃:

-[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;}
4

2 回答 2

14

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

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

尝试这个:

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

        return YES;
    }
    return NO;
}

更新

斯威夫特 2.0 版本

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

    let translation = panGestureRecognizer.translationInView(self.superview)
}
于 2013-08-11T16:11:25.297 回答
4

在你的:

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer

尝试改变:

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

至:

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

希望这可以帮助。

于 2013-05-02T05:12:37.580 回答