我有一个功能:
@objc func handleLongPress(data: SkelbimasModel, _ gestureRecognizer:UIGestureRecognizer){
if gestureRecognizer.state != .began{
return
}
let touchPoint:CGPoint = gestureRecognizer.location(in: self.Map)
let touchMapCoordinate:CLLocationCoordinate2D =
self.Map.convert(touchPoint, toCoordinateFrom: self.Map)
let annot:MKPointAnnotation = MKPointAnnotation()
annot.coordinate = touchMapCoordinate
self.resetTracking()
self.Map.addAnnotation(annot)
skelbimai.append(annot)
print(skelbimai)
self.Map.removeGestureRecognizer(gestureRecognizer)
Label.isHidden = true
}
我的SkelbimbasModel继承了NSObject:
类SkelbimasModel:NSObject {...}
并且没有调用该函数:
func addLongPressGesture(){
let longPressRecogniser:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target:self , action:#selector(ViewController.handleLongPress(data:_:)))
longPressRecogniser.minimumPressDuration = 0.5 //user needs to press for 1 seconds
self.Map.addGestureRecognizer(longPressRecogniser)
}
当我从以下函数中删除SkelbimasModel属性时:
@objc func handleLongPress(_ gestureRecognizer:UIGestureRecognizer)
工作正常。如何将函数声明为selector,以传递SkalbimasModel属性?
答案 0 :(得分:1)
选择器签名必须是以下之一。
@IBAction func myActionMethod()
@IBAction func myActionMethod(_ sender: UIGestureRecognizer)
官方文档
手势识别器具有关联的一个或多个目标 - 动作对 用它。如果有多个目标 - 动作对,它们是离散的, 而不是累积的。对手势的识别导致调度 对于每个关联对的目标的动作消息。该 调用的操作方法必须符合以下之一 签名:
@IBAction func myActionMethod()
@IBAction func myActionMethod(_ sender:UIGestureRecognizer)
如果您不想区分视图,可以查看视图标记。
let tag = gestureRecognizer.view?.tag
答案 1 :(得分:1)
以下是您的问题的答案:UIGestureRecognizer
调用的UIGestureRecognizer操作方法必须符合以下签名之一:
@IBAction func myActionMethod()
// or func myActionMethod()
@IBAction func myActionMethod(_ sender: UIGestureRecognizer)
// or func myActionMethod(_ sender: UIGestureRecognizer)
您只能传递UIGestureRecognizer