Swift 将缺少的三角形(工具提示)添加到详图索引自定义MKAnnotationView子类

Swift 将缺少的三角形(工具提示)添加到详图索引自定义MKAnnotationView子类,swift,mapkit,mkannotationview,Swift,Mapkit,Mkannotationview,我创建了一个自定义的MKAnnotationView,它可以很好地工作,但是它会弹出一个矩形,没有三角形(与漫画中有人说话时的三角形相同),它会从图钉中弹出。 有没有办法自动添加三角形 以下是我的MKAnnotationView子类: class ShikmimAnnotationView: MKAnnotationView{ let selectedLabel:UILabel = UILabel.init(frame:CGRect(x: 0, y: 0, width: 140, height

我创建了一个自定义的
MKAnnotationView
,它可以很好地工作,但是它会弹出一个矩形,没有三角形(与漫画中有人说话时的三角形相同),它会从图钉中弹出。 有没有办法自动添加三角形

以下是我的MKAnnotationView子类:

class ShikmimAnnotationView: MKAnnotationView{

let selectedLabel:UILabel = UILabel.init(frame:CGRect(x: 0, y: 0, width: 140, height: 40))

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(false, animated: animated)
    if(selected)
    {
        // Do customization, for example:
        //let defaults = UserDefaults.standard

        selectedLabel.font = UIFont(name: "Heebo-Regular.ttf", size: 18)
        selectedLabel.text = "(גדליהו אלון 13, ירושלים\n     (3 דק' הגעה"
        selectedLabel.numberOfLines = 3
        selectedLabel.sizeToFit()
        selectedLabel.textColor = UIColor.white
        selectedLabel.textAlignment = .center
        selectedLabel.backgroundColor = UIColor.darkGray
        selectedLabel.layer.borderColor = UIColor.white.cgColor
        selectedLabel.layer.borderWidth = 2
        selectedLabel.layer.cornerRadius = 5
        selectedLabel.layer.masksToBounds = true

        selectedLabel.center.x = 0.5 * self.frame.size.width;
        selectedLabel.center.y = -0.5 * selectedLabel.frame.height;
        self.addSubview(selectedLabel)
        self.image = UIImage(named: "map_pin_next_stop-2.png")

    }
    else
    {
        selectedLabel.removeFromSuperview()
    }
}
}
以下是我如何在VC中初始化它:

   internal func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if let annotation = annotation as? MyLocation {
        let identifier = "reuse"
        var view: ShikmimAnnotationView
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier){
            dequeuedView.annotation = annotation
            view = dequeuedView as! ShikmimAnnotationView
        } else {
            view = ShikmimAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.image = UIImage(named: "map_pin_next_stop-2.png")
            view.canShowCallout = false
            //view.calloutOffset = CGPoint(x: -5, y: 5)
            //view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
        }

        return view
    }
    return nil
}

你可以用BezierPath画这个三角形。这是这个视图的类。您可以在storyboard中设置视图的大小,也可以在ShikmimAnnotationView类中以编程方式设置视图的大小

class TriangleView: UIView {

let shapeLayer = CAShapeLayer()

override func drawRect(rect: CGRect) {

    // Get Height and Width
    let layerHeight = self.layer.frame.height
    let layerWidth = self.layer.frame.width

    // Create Path
    let bezierPath = UIBezierPath()

    // Draw Points
    bezierPath.moveToPoint(CGPoint(x: 0, y: 0))
    bezierPath.addLineToPoint(CGPoint(x: layerWidth, y: 0))
    bezierPath.addLineToPoint(CGPoint(x: layerWidth / 2, y: layerHeight))
    bezierPath.addLineToPoint(CGPoint(x: 0, y: 0))
    bezierPath.closePath()

    // Apply Color
    UIColor(red: (2/255), green: (35/255), blue: (73/255), alpha: 1).setFill()
    bezierPath.fill()

    // Mask to Path

    shapeLayer.path = bezierPath.CGPath
    self.layer.mask = shapeLayer
  }
}

确实需要为对annotationview所做的较小更改创建自己的类吗?谢谢,这很公平。我确实希望有一个神奇的内置MapKit功能:)但有时你只需要为它们编程就可以了。