自定义注解视图找不到按钮点击事件怎么办?
How to do custom annotation view not find button click event?
我会尝试找出自定义注释中的按钮单击事件 view.How 这样做 This.In 在此找不到按钮单击事件。请给点提示。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let overlays = self.mapVW.overlays
self.mapVW.removeOverlays(overlays)
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
let customeView = view.annotation as! Artwork
let viewCustome = Bundle.main.loadNibNamed("mapPopUp", owner: nil, options: nil)
let callOutView = viewCustome![0] as! CustomCalloutView
callOutView.lblLocationName.text = customeView.title
callOutView.lblCategory.text = customeView.category
callOutView.lblDistance.text = customeView.distance
let button = UIButton(frame: callOutView.lblLocationName.frame)
button.addTarget(self, action: #selector(GayGuideViewController.btnRouteView(sender:)), for: .touchUpInside)
callOutView.addSubview(button)
callOutView.center = CGPoint(x: view.bounds.size.width / 5, y: -callOutView.bounds.size.height*0.52)
view.addSubview(callOutView)
mapVW.setCenter((view.annotation?.coordinate)!, animated: true)
}
提前致谢。
我在自定义弹出视图中使用 hitTest 方法处理点击事件。那个时候我找到了命中点添加通知观察者并执行我的动作事件。
class CAnnotationView: MKAnnotationView
{
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if (hitView != nil)
{
self.superview?.bringSubview(toFront: self)
}
return hitView
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside: Bool = rect.contains(point)
if(!isInside)
{
for view in self.subviews
{
isInside = view.frame.contains(point)
if isInside
{
let dictionary = NSMutableDictionary()
dictionary.setValue(self.annotation?.coordinate.latitude, forKey: "lat")
dictionary.setValue(self.annotation?.coordinate.longitude, forKey: "long")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil, userInfo: dictionary as? [AnyHashable : Any])
break
}
}
}
return isInside
}
}
override func viewDidLoad()
{
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(notificationForRoute(noti:)), name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil)
}
func notificationForRoute(noti : NSNotification)
{
let dict = noti.userInfo! as NSDictionary
let lat = dict.value(forKey: "lat")
let long = dict.value(forKey: "long")
let coordinate = CLLocationCoordinate2D(latitude: lat as! CLLocationDegrees, longitude: long as! CLLocationDegrees)
let overlays = self.mapVW.overlays
self.mapVW.removeOverlays(overlays)
route(dest: coordinate )
}
我会尝试找出自定义注释中的按钮单击事件 view.How 这样做 This.In 在此找不到按钮单击事件。请给点提示。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let overlays = self.mapVW.overlays
self.mapVW.removeOverlays(overlays)
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
let customeView = view.annotation as! Artwork
let viewCustome = Bundle.main.loadNibNamed("mapPopUp", owner: nil, options: nil)
let callOutView = viewCustome![0] as! CustomCalloutView
callOutView.lblLocationName.text = customeView.title
callOutView.lblCategory.text = customeView.category
callOutView.lblDistance.text = customeView.distance
let button = UIButton(frame: callOutView.lblLocationName.frame)
button.addTarget(self, action: #selector(GayGuideViewController.btnRouteView(sender:)), for: .touchUpInside)
callOutView.addSubview(button)
callOutView.center = CGPoint(x: view.bounds.size.width / 5, y: -callOutView.bounds.size.height*0.52)
view.addSubview(callOutView)
mapVW.setCenter((view.annotation?.coordinate)!, animated: true)
}
提前致谢。
我在自定义弹出视图中使用 hitTest 方法处理点击事件。那个时候我找到了命中点添加通知观察者并执行我的动作事件。
class CAnnotationView: MKAnnotationView
{
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if (hitView != nil)
{
self.superview?.bringSubview(toFront: self)
}
return hitView
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside: Bool = rect.contains(point)
if(!isInside)
{
for view in self.subviews
{
isInside = view.frame.contains(point)
if isInside
{
let dictionary = NSMutableDictionary()
dictionary.setValue(self.annotation?.coordinate.latitude, forKey: "lat")
dictionary.setValue(self.annotation?.coordinate.longitude, forKey: "long")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil, userInfo: dictionary as? [AnyHashable : Any])
break
}
}
}
return isInside
}
}
override func viewDidLoad()
{
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(notificationForRoute(noti:)), name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil)
}
func notificationForRoute(noti : NSNotification)
{
let dict = noti.userInfo! as NSDictionary
let lat = dict.value(forKey: "lat")
let long = dict.value(forKey: "long")
let coordinate = CLLocationCoordinate2D(latitude: lat as! CLLocationDegrees, longitude: long as! CLLocationDegrees)
let overlays = self.mapVW.overlays
self.mapVW.removeOverlays(overlays)
route(dest: coordinate )
}