iOS 滑动手势时应用崩溃?

iOS app crashing during swipe gesture?

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

我已经搜索了解决方案,但似乎无法找出崩溃的原因。我已将我的“:”添加到我的操作中,并将 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

        }

    }

}

您必须在加载函数的视图之外声明您的函数。试试看。