GestureRecognizer:在 SIGABRT 中的地图结果上放置图钉

Posted

技术标签:

【中文标题】GestureRecognizer:在 SIGABRT 中的地图结果上放置图钉【英文标题】:GestureRecognizer: placing pin on Map results in SIGABRT 【发布时间】:2016-02-07 12:20:13 【问题描述】:

我正在尝试向地图添加手势识别器;但是,当我启动程序时,它会因信号 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:]:无法识别的选择器发送到实例 0x7f882a66d4d0 2016-02-07 13:30:17.968 SM[25924:2977301] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[SM.ViewController3 handleLongPress:]:无法识别的选择器发送到实例 0x7f882a66d4d0” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010b046e65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010d042deb objc_exception_throw + 48 2核心基础0x000000010b04f48d-[NSObject(NSObject)不识别选择器:]+205 3 核心基础 0x000000010af9c90a ___转发___ + 970 4 核心基础 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 核心基础 0x000000010af72367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 12 核心基础 0x000000010af722d7 __CFRunLoopDoObservers + 391 13 核心基础 0x000000010af67f2b __CFRunLoopRun + 1147 14 核心基础 0x000000010af67828 CFRunLoopRunSpecific + 488 15 图形服务 0x000000010e75cad2 GSEventRunModal + 161 16 UIKit 0x000000010bb1f610 UIApplicationMain + 171 17 SM 0x000000010add1a8d 主 + 109 18 libdyld.dylib 0x000000010f0b192d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

类本身是GestureRecognizer的委托。

【问题讨论】:

请发布实际的错误信息,并func handleLongPress(sender:....) 请贴出包括handleLongPress函数在内的全部代码。 【参考方案1】:

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)

【讨论】:

【参考方案2】:

由于 MKMapView 已经有手势识别器,请尝试实现 UIGestureRecognizerDelegate 这个方法:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
    shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool

    return true

【讨论】:

以上是关于GestureRecognizer:在 SIGABRT 中的地图结果上放置图钉的主要内容,如果未能解决你的问题,请参考以下文章

用Swift GestureRecognizer 的几个注意点

tableView 在执行 didSelectRowAtIndexPath 之前由于有一个gestureRecognizer而被隐藏

检查哪个视图发送gestureRecognizer

使用gestureRecognizer设置UIImageView的图像

GestureRecognizer 没有响应点击

从 Interface Builder 连接时,GestureRecognizer 操作未触发