首页 文章

自定义标注视图按钮映射

提问于
浏览
0

我在MapView上工作,点击任何自定义注释引脚,我显示自定义标注视图(从xib文件加载) .

从这个自定义标注,我有一个UIButton,我已经可以检测到点击这个按钮,但我想在 Map 上访问,如:基本标注中的: . ? . ViewCalloutAccessoryView .

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView,  {

    if view.annotation!.isKind(of: MKUserLocation.self){
        return
    }

    let customView = (Bundle.main.loadNibNamed("CustomCalloutView", owner: self, options: nil))?[0] as! CustomCalloutView;
    let calloutViewFrame = customView.frame;
    customView.frame = CGRect(x: -calloutViewFrame.size.width/2.23, y: -calloutViewFrame.size.height+10, width: 315, height: 170)

    view.addSubview(customView)

    let region = MKCoordinateRegion(center: pinToZoomOn!.coordinate, span: span)

    mapView.setRegion(region, animated: true)
}

路线是从经典标注中正确计算出来的,但我不知道如何从我的自定义标注按钮访问我的 Map .

我的CustomCalloutViewClass:

import UIKit
import MapKit

class CustomCalloutView: MKAnnotationView {

@IBOutlet weak var goButton: UIButton!

@IBAction func goButton(_ sender: AnyObject) {
    print("Button clicked sucessfully")
}

// MARK: - Detect taps on callout

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let hitView = super.hitTest(point, with: event)
    if hitView != nil {
        superview?.bringSubview(toFront: self)
    }
    return hitView
}

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    let rect = self.bounds
    var isInside = rect.contains(point)
    if !isInside {
        for view in subviews {
            isInside = view.frame.contains(point)
            if isInside {
                break
            }
        }
    }
    return isInside
}
}

如果有人有想法会有所帮助,我会坚持这个问题 .

先感谢您 .

2 回答

  • 0

    选项1:在传递给CustomCalloutView的闭包中捕获MKMapView实例

    添加将在按钮操作上调用的闭包 . 关闭将捕获 MKMapView 实例,您将能够在我们内部 .

    class CustomCalloutView: MKAnnotationView {
        var didTapGoButton: (() -> Void)?
    
        @IBAction func goButton(_ sender: AnyObject) {
            didTapGoButton?()
        }
    }
    

    选项2:添加对MKMapView的弱引用作为标注的属性

    这不是一个干净的解决方案,但在某些情况下它可能是一种选择 . 您只需创建一个弱属性,在 CustomCalloutView 中存储对 MKMapView 实例的引用

    class CustomCalloutView: MKAnnotationView {
        weak var mapView: MKMapView?
    }
    

    配置

    这就是为两种解决方案连接 CustomCalloutView 的方法 . 请记住使用swift捕获列表来捕获对 MKMapView 实例的弱引用 . 没有它,您可以创建一个强大的参考周期 .

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)  {
        // ...
        let customView = (Bundle.main.loadNibNamed("CustomCalloutView", owner: self, options: nil))?[0] as! CustomCalloutView;
        // Option 1
        customView.didTapGoButton = { [weak mapView ] in 
            print(mapView?.annotations.count) 
        }
        // Option 2
        customView.mapView = mapView
        // ...
    }
    
  • 0

    非常感谢你的帮助 !

    我使用第一个选项来捕获MKMapView实例 .

    如果有人遇到同样的问题,你可以使用第一个选项并在你的didSelect MapView函数中添加它:

    let selectedLoc = view.annotation
    print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")
    
    let location = view.annotation as! YourCustomClassType
    let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
    customView.didTapGoButton = { location.mapItem().openInMaps(launchOptions: launchOptions) }
    

    谢谢

相关问题