在 Swift 中创建动态 MKAnnotationView

Creating a dynamic MKAnnotationView in Swift

目前,我知道如何创建带有静态图钉的 MKAnnotationView,即添加的图像。

有谁知道或有任何资源知道如何创建一个会改变颜色或在其中显示一个数字的图钉,该数字会根据有关业务的信息而改变?

例如,我想让一个图钉在营业结束时显示为红色,在营业时显示为绿色。甚至可能在 pin 里面有一个美元符号来告诉用户它有多贵。

编辑

我创建了一个名为 CustomPin 的 class,它采用 MKAnnotation 协议。此外,我不希望拥有可以更改的自定义 MKAnnotationView 图像。这是否意味着我必须在数组中为 MKAnnotationView 添加多张图像,并让它在每次有关业务的详细信息发生变化时更改图像?

提前致谢!

您必须以编程方式执行此操作。以下代码可能对您有所帮助: 初始化 pin 并为其指定类型,例如打开、关闭、昂贵等。

  MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
  newAnnotation.type = @"opened";  // <-- set type in annotation
  [self.mapView addAnnotation:newAnnotation];

在你的 (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) 注释方法中做一个简单的检查

 MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation;
    if ([mvAnn.type isEqualToString:@"opened"])
    {
        annView.pinColor = MKPinAnnotationColorGreen;
    }
    else if ([mvAnn.type isEqualToString:@"closed"])
    {
        annView.pinColor = MKPinAnnotationColorRed;
    }
    else
    {
        annView.pinColor = MKPinAnnotationColorOrange;
    }

您可以通过简单的继承来创建自定义 MKAnnotationView。您可以使用此 class 从委托方法创建注释视图。

这是一个例子。

class KDAnnotationView: MKAnnotationView {

    let titleLabel = UILabel()

    convenience init(annotation: MKAnnotation?) {
        self.init(annotation: annotation, reuseIdentifier: "indetifier")
        self.canShowCallout = false
        self.frame = CGRectMake(0, 0, 75.0, 85.0)
        self.backgroundColor = UIColor.clearColor()
        self.centerOffset = CGPointMake(0, 0)

        self.titleLabel.backgroundColor = UIColor.clearColor()
        self.titleLabel.textColor = UIColor.blackColor()
        self.titleLabel.font = UIFont.systemFontOfSize(16.0)
        self.addSubview(self.titleLabel)
    }


    override func layoutSubviews() {
        super.layoutSubviews()

        var frame = CGRectInset(self.bounds, 5.0, 5.0)
            frame.size.height = 20.0
        self.titleLabel.frame = frame
    }


    override func drawRect(rect: CGRect) {
        super.drawRect(rect)


        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: CGRectGetMinX(rect), y: CGRectGetMinY(rect)))
        path.addLineToPoint(CGPoint(x: CGRectGetMaxX(rect), y: CGRectGetMinY(rect)))
        path.addLineToPoint(CGPoint(x: CGRectGetMaxX(rect), y: CGRectGetMaxY(rect) - 10.0))
        path.addLineToPoint(CGPoint(x: CGRectGetMidX(rect) + 5.0, y: CGRectGetMaxY(rect) - 10.0))
        path.addLineToPoint(CGPoint(x: CGRectGetMidX(rect), y: CGRectGetMaxY(rect)))
        path.addLineToPoint(CGPoint(x: CGRectGetMidX(rect) - 5.0, y: CGRectGetMaxY(rect) - 10.0))
        path.addLineToPoint(CGPoint(x: CGRectGetMinX(rect), y: CGRectGetMaxY(rect) - 10.0))
        path.closePath()
        UIColor.lightGrayColor().setStroke()
        UIColor.whiteColor().setFill()
        path.stroke()
        path.fill()

    }


    //MARK: - Public Methods
    func setText(text:String) {
        self.titleLabel.text = text
    }
}