GestureRecognizer:在 SIGABRT 中将图钉放在地图上
GestureRecognizer: placing pin on Map results in SIGABRT
我正在尝试向地图添加手势识别器;但是,当我启动程序时,它因信号 SIGABRT 而崩溃。你能帮我弄清楚吗,因为我对 iOS 开发真的很陌生?
override func viewDidLoad() {
super.viewDidLoad()
v.hidden=true
let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
lpgr.delegate=self
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
self.theMap.addGestureRecognizer(lpgr)
}
func action(gestureRecognizer:UIGestureRecognizer){
var touchPoint = gestureRecognizer.locationInView(theMap)
var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
theMap.addAnnotation(annotation)
}
2016-02-07 13:30:17.963 SM[25924:2977301] -[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0
2016-02-07 13:30:17.968 SM[25924:2977301] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010b046e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d042deb objc_exception_throw + 48
2 CoreFoundation 0x000000010b04f48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010af9c90a ___forwarding___ + 970
4 CoreFoundation 0x000000010af9c4b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010c007e73 _UIGestureRecognizerSendTargetActions + 153
6 UIKit 0x000000010c0044e5 _UIGestureRecognizerSendActions + 162
7 UIKit 0x000000010c0024e2 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
8 UIKit 0x000000010c00a9a0 ___UIGestureRecognizerUpdate_block_invoke904 + 79
9 UIKit 0x000000010c00a83e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
10 UIKit 0x000000010bff8101 _UIGestureRecognizerUpdate + 2634
11 CoreFoundation 0x000000010af72367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
12 CoreFoundation 0x000000010af722d7 __CFRunLoopDoObservers + 391
13 CoreFoundation 0x000000010af67f2b __CFRunLoopRun + 1147
14 CoreFoundation 0x000000010af67828 CFRunLoopRunSpecific + 488
15 GraphicsServices 0x000000010e75cad2 GSEventRunModal + 161
16 UIKit 0x000000010bb1f610 UIApplicationMain + 171
17 SM 0x000000010add1a8d main + 109
18 libdyld.dylib 0x000000010f0b192d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
class本身就是GestureRecognizer
的委托。
由于 MKMapView 已经有手势识别器,尝试实现 UIGestureRecognizerDelegate 的这个方法:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
func
应该命名为 handleLongPress
而不是命名为 action
:
即
func handleLongPress(gestureRecognizer:UIGestureRecognizer)
{
var touchPoint = gestureRecognizer.locationInView(theMap)
var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
theMap.addAnnotation(annotation)
}
我正在尝试向地图添加手势识别器;但是,当我启动程序时,它因信号 SIGABRT 而崩溃。你能帮我弄清楚吗,因为我对 iOS 开发真的很陌生?
override func viewDidLoad() {
super.viewDidLoad()
v.hidden=true
let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
lpgr.delegate=self
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
self.theMap.addGestureRecognizer(lpgr)
}
func action(gestureRecognizer:UIGestureRecognizer){
var touchPoint = gestureRecognizer.locationInView(theMap)
var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
theMap.addAnnotation(annotation)
}
2016-02-07 13:30:17.963 SM[25924:2977301] -[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0 2016-02-07 13:30:17.968 SM[25924:2977301] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0' *** First throw call stack: ( 0 CoreFoundation 0x000000010b046e65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010d042deb objc_exception_throw + 48 2 CoreFoundation 0x000000010b04f48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010af9c90a ___forwarding___ + 970 4 CoreFoundation 0x000000010af9c4b8 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010c007e73 _UIGestureRecognizerSendTargetActions + 153 6 UIKit 0x000000010c0044e5 _UIGestureRecognizerSendActions + 162 7 UIKit 0x000000010c0024e2 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843 8 UIKit 0x000000010c00a9a0 ___UIGestureRecognizerUpdate_block_invoke904 + 79 9 UIKit 0x000000010c00a83e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342 10 UIKit 0x000000010bff8101 _UIGestureRecognizerUpdate + 2634 11 CoreFoundation 0x000000010af72367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 12 CoreFoundation 0x000000010af722d7 __CFRunLoopDoObservers + 391 13 CoreFoundation 0x000000010af67f2b __CFRunLoopRun + 1147 14 CoreFoundation 0x000000010af67828 CFRunLoopRunSpecific + 488 15 GraphicsServices 0x000000010e75cad2 GSEventRunModal + 161 16 UIKit 0x000000010bb1f610 UIApplicationMain + 171 17 SM 0x000000010add1a8d main + 109 18 libdyld.dylib 0x000000010f0b192d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
class本身就是GestureRecognizer
的委托。
由于 MKMapView 已经有手势识别器,尝试实现 UIGestureRecognizerDelegate 的这个方法:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
func
应该命名为 handleLongPress
而不是命名为 action
:
即
func handleLongPress(gestureRecognizer:UIGestureRecognizer)
{
var touchPoint = gestureRecognizer.locationInView(theMap)
var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
theMap.addAnnotation(annotation)
}