我尝试了每一次搜索,但在一周内没有发现任何相似之处。 我正在创建一个显示表视图的应用程序。单元格(由自定义类和Interface Builder制作)可以使用属于Cell类的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 EVEWHERE(在单元格类中,在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;}
答案 0 :(得分:14)
我通过检查您收到的gestureRecognizer的类型解决了这个问题。
有时我会收到“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)
}
答案 1 :(得分:4)
在你的:
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
尝试改变:
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
要:
CGPoint translation = [gestureRecognizer locationInView:[self superview]];
希望这有帮助。