仅当我点击自定义注释视图的左上角时,才会显示Mapview标注

时间:2016-02-22 16:52:37

标签: ios swift mapkit

我有一个带有自定义注释视图和标注视图的MapKit实现。出于一些非常恼人的原因(我已经坚持了好几天......),如果我点击注释视图的左上角,则会显示标注视图。

我为我的注释视图实现了pointInside函数,iPhone确实看到我在注释的中间点击,它只是在点击之后没有做任何事情。

我该如何解决这个问题?我需要能够点击自定义注释视图的任何位置来查看标注。

如果您对如何解决此问题有任何疑问,请提供帮助。

修改

这是我的viewForAnnotation实现

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if (annotation is MKUserLocation) {
            return nil
        }

        var customCalloutView = CustomCalloutView()
        let rentalAnnotation = annotation as? RentalAnnotation
            let identifier = "pin"
            var customAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            customAnnotationView.userInteractionEnabled = true
            customAnnotationView.centerOffset = CGPointMake(0, -customAnnotationView.loadedView.frame.size.height / 2)
            customAnnotationView.calloutOffset = CGPointMake(25, -2)
            let widthConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 200)
            customCalloutView.addConstraint(widthConstraint)
            let heightConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 200)
            customCalloutView.addConstraint(heightConstraint)

        if let annotation = annotation as? RentalAnnotation {

            if let dequeuedView = rentalsMapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? CustomAnnotationView {
                dequeuedView.annotation = annotation
                if let label = dequeuedView.label {
                    label.text = "$ \(annotation.price)"
                }
                dequeuedView.calloutOffset = CGPointMake(25, -2)
                dequeuedView.canShowCallout = true
                dequeuedView.centerOffset = CGPointMake(0, -customAnnotationView.loadedView.frame.size.height / 2)
                dequeuedView.detailCalloutAccessoryView = customCalloutView
                customAnnotationView = dequeuedView
            } else {
                customAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                customAnnotationView.label?.text = "$ \(annotation.price)"
                customAnnotationView.canShowCallout = true
                customAnnotationView.centerOffset = CGPointMake(0, -customAnnotationView.loadedView.frame.size.height / 2)
                customAnnotationView.calloutOffset = CGPointMake(25, -2)
                customAnnotationView.detailCalloutAccessoryView = customCalloutView
 = customCalloutView

            }
        }
            return customAnnotationView

        // if annotation is nil, then return a nil for MKAnnotationView
    }

这也是我的MKAnnotationView的自定义子类

class CustomAnnotationView: MKAnnotationView {

    @IBOutlet weak var label: UILabel?
    var loadedView = UIView()

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        loadedView = (NSBundle.mainBundle().loadNibNamed("CustomAnnotationViewDesign", owner: self, options: nil)[0] as? UIView)!
        self.addSubview(loadedView)
        label?.userInteractionEnabled = true
    }

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        if point.x > loadedView.frame.minX && point.x < loadedView.frame.maxX && point.y > loadedView.frame.minY && point.y < loadedView.frame.maxY {
            print("Point inside")
            return true
        } else {
            print("Point not inside")
            return false
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
}

0 个答案:

没有答案