在我正在开发的开发者课程中使用滑动手势练习。我已经按照我的知识执行了这些步骤,但似乎每次执行滑动(向右或向上)时,我的应用程序都会崩溃。
我已经搜索了一个解决方案,但似乎无法找出它崩溃的原因。我添加了我的" :"我的行动并将 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)
答案 0 :(得分:2)
声明在viewDidLoad外部滑动,它不应该崩溃
{{1}}
答案 1 :(得分:1)
你必须在视图之外声明你的函数加载函数。试试吧。