iOS 应用程序在滑动手势期间崩溃?

标签 ios swift unrecognized-selector uiswipegesturerecognizer

在我参加的开发人员类(class)中练习滑动手势。据我所知,我已按照这些步骤操作,但似乎每次我执行滑动(向右或向上)时,我的应用程序都会崩溃。

我已经搜索了一个解决方案,但似乎无法找出崩溃的原因。我已将“:”添加到我的操作中,并将 UIGestureRecognizerDelegate 添加到我的类中。

应用程序会运行,只是在我执行滑动时崩溃。
这是我的代码:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeUp)


    func swiped(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

            case UISwipeGestureRecognizerDirection.Right:
                print("swiped right")
            case UISwipeGestureRecognizerDirection.Up:
                print("swiped up")
            default:
                break

            }

        }

    }



}

我收到的错误信息如下。

2015-10-26 10:59:52.944 Swipes and Shakes[2764:444281] -[Swipes_and_Shakes.ViewController swiped:]: unrecognized selector sent to instance 0x7fea2a58bcd0
2015-10-26 10:59:52.950 Swipes and Shakes[2764:444281] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swipes_and_Shakes.ViewController swiped:]: unrecognized selector sent to instance 0x7fea2a58bcd0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010dfb2f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010fccedeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010dfbb56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010df08eea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010df08a98 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ecae37c _UIGestureRecognizerSendTargetActions + 153
    6   UIKit                               0x000000010ecaacf6 _UIGestureRecognizerSendActions + 162
    7   UIKit                               0x000000010eca8cf3 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    8   UIKit                               0x000000010ecb0c9f ___UIGestureRecognizerUpdate_block_invoke877 + 79
    9   UIKit                               0x000000010ecb0b3d _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    10  UIKit                               0x000000010ec9edb1 _UIGestureRecognizerUpdate + 2634
    11  UIKit                               0x000000010e840684 -[UIWindow _sendGesturesForEvent:] + 1137
    12  UIKit                               0x000000010e8418ba -[UIWindow sendEvent:] + 849
    13  UIKit                               0x000000010e7f101a -[UIApplication sendEvent:] + 263
    14  UIKit                               0x000000010e7cb8c7 _UIApplicationHandleEventQueue + 6844
    15  CoreFoundation                      0x000000010dedf011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x000000010ded4f3c __CFRunLoopDoSources0 + 556
    17  CoreFoundation                      0x000000010ded43f3 __CFRunLoopRun + 867
    18  CoreFoundation                      0x000000010ded3e08 CFRunLoopRunSpecific + 488
    19  GraphicsServices                    0x00000001125d7ad2 GSEventRunModal + 161
    20  UIKit                               0x000000010e7d1031 UIApplicationMain + 171
    21  Swipes and Shakes                   0x000000010ddd326d main + 109
    22  libdyld.dylib                       0x00000001107d092d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

最佳答案

声明在 viewDidLoad 外部滑动,它不应该崩溃

class ViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeUp)
}

func swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right:
            print("swiped right")
        case UISwipeGestureRecognizerDirection.Up:
            print("swiped up")
        default:
            break

        }

    }

}

关于iOS 应用程序在滑动手势期间崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349101/

相关文章:

swift - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序。无法识别的选择器发送到实例

Objective-C 类别不被识别?

iphone - 在头文件中使用变量和常量定义进行本地化

objective-c - ASIHTTPRequestErrorDomain 代码=8。无法将文件从临时目录移动到文档

ios - 从 Swift 中的画廊中挑选图像

ios - [UIImageView CGImage] : unrecognized selector sent to instance 0x1783e5b00

ios - 用于在 Swift 中使用通配符的清晰单行单词的正则表达式

javascript - HTML 中的 UINavigationController 类型菜单

swift - 如何在我现有的 XIB 中调用 XIB?

ios - 为什么 CustomStringConvertible 协议(protocol)不适用于 Int?