UITapGestureRecognizer导致应用程序崩溃

时间:2014-10-28 13:54:03

标签: objective-c crash uitapgesturerecognizer swipe-gesture

UITapGestureRecognizer导致应用在完成滑动时崩溃。在非常短的距离内完成滑动不会造成任何问题,但是在较长距离上完成的滑动会产生错误:

 -[UITapRecognizer name]: unrecognized selector sent to instance 0x17ee27c0

其中0x17ee27c0是随机值。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapRecognizer name]: unrecognized selector sent to instance 0x17ee27c0'
*** First throw call stack:
(0x2cb7dc1f 0x3a328c8b 0x2cb83039 0x2cb80f57 0x2cab2df8 0x2feee1c1 0x2d7d01cf 0x3024822d 0x300671ad 0x30066bcd 0x3003d3dd 0x302b0c29 0x3003be39 0x2cb44377 0x2cb43787 0x2cb41ded 0x2ca90211 0x2ca90023 0x33e890a9 0x3009c1d1 0xdca87 0x3a8a8aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

UITapRecognizer *(0x17ee27c0)来自tapGestureRecognizer._imp

这是在运行iOS 8.1的多台设备上发生的。源代码在Xcode 6上编译。

以下是我如何声明UITapGestureRecognizer:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[myView addGestureRecognizer:tapGestureRecognizer];      

以下是方法:

- (void)viewTapped:(UITapGestureRecognizer *)sender { }

UITapGestureRecognizer放置在SKView上;

这是堆栈跟踪: http://imageshack.com/a/img903/3622/X9QFX0.png

4 个答案:

答案 0 :(得分:1)

我在我的一个SpriteKit游戏中遇到了同样的问题,这是在场景转换过程中使用手势时引起的,我通过将gestureRecognizer.enable属性(documentation)设置为{{1}来解决它在过渡之前。

答案 1 :(得分:0)

您正在尝试在name个实例上运行名为UITapGestureRecognizer的选择器,但UITapGestureRecognizer没有这样的选择器,这就是您的程序为何的原因崩溃。检查您呼叫name的位置(可能在viewTapped内),并确保将其应用于正确的对象。

答案 2 :(得分:0)

你确定ViewTapped:期待争论。

答案 3 :(得分:0)

您可以使用强制转换作为示例:action as MyTapGesture

var gestureRecognizer = new MyTapGesture((action) =>
            {
                myEvent(action as MyTapGesture);
            });
            gestureRecognizer.Name = name;

并将MyTapGesture类还原为UITapGestureRecognizer

using System;
using UIKit;

namespace ETracker.Libraries
{
    public class MyTapGesture: UITapGestureRecognizer
    {
        public string Name { get; set; }

        public MyTapGesture()
        {
        }

        public MyTapGesture(Action<UITapGestureRecognizer> action) : base(action)
        {
        }
    }
}