【问题标题】:Custom View Xib for MKAnnotationView button tapMKAnnotationView 按钮点击的自定义视图 Xib
【发布时间】:2018-01-08 12:30:30
【问题描述】:

我正在制作一个自定义的 MKAnnotationView,并且我在 xib 中有一个按钮,我想在里面进行修饰时获得该按钮。

尽管有相同问题的问题,但没有一个与我的代码一起使用。 我有以下。

class CustomCalloutView: MKAnnotationView {

     var didTapGoButton: (() -> Void)?

    // MARK: - Detect taps on callout

    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
                {
                    break;
                }
            }
        }
        return isInside;
    }
    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
    }

    @IBAction func viewEventDetails(_ sender: Any) {
        didTapGoButton?()
    }

 func configureCell(events: Events) {

  eventName.text = events.eventName

 }
}

视图控制器

class EventAnnotation : MKPointAnnotation {
    var myEvent:Events?
}


    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let annotation = view.annotation else { return }
        if view.annotation!.isKind(of: MKUserLocation.self){
            return
        }
         if let eventAnnotation = view.annotation as? EventAnnotation {
                    let theEvent = eventAnnotation.myEvent
                    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-7, width: 315, height: 298)
                    customView.configureCell(events: theEvent!)
                    customView.didTapGoButton = { [weak mapView ] in
                        print(theEvent?.eventName ?? "noname")
                    }
                    view.addSubview(customView)

                }
    }


    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView)
    {
        for childView:AnyObject in view.subviews{
            childView.removeFromSuperview();
        }
    }

所以当我点击按钮时,什么都没有打印出来!

知道我在代码中的错误在哪里吗?

【问题讨论】:

    标签: ios swift swift3 mkannotationview


    【解决方案1】:

    我解决了它,想法是将 customView 定位在 pinAnnotationView 图像的边界,因为它的任何点击侧都将被视为 didSelect 方法调用,因此在检测到点击之前一直使用框架,它可以在自定义视图或视图控制器

        customView.frame = CGRect(x: -calloutViewFrame.size.width/2.23, y: calloutViewFrame.size.height, width: 315, height: 298)
    

    这里有一个演示来说明情况

    customPinAnnotationButton

    【讨论】:

    • 在视图控制器中?
    • 我无法通过这种方式获取Events的实例。
    • 然后在自定义视图中添加
    • 两种方式都不执行按下的功能。
    • 别指望这个。 customView.didTapGoButton = { [weak mapView] in print(theEvent?.eventName ?? "noname") }。当你点击按钮时运行它是在运行中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多