在Swift中创建动态MKAnnotationView [英] Creating a dynamic MKAnnotationView in Swift

查看:322
本文介绍了在Swift中创建动态MKAnnotationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我知道如何使用静态图钉(添加的图像)创建MKAnnotationView.

Currently, I know how to create a MKAnnotationView with a static pin, an image that's added.

是否有人知道或拥有任何资源,如何创建会改变颜色或内部显示数字会根据业务信息而改变的图钉?

Does anyone know, or have any resources, on how to create a pin that would change colors or have a number displayed inside of it that would change depending on information about the business?

例如,我希望在业务关闭时为红色,而在业务开放时为绿色.甚至在大头针里面甚至有一个美元符号,告诉用户它有多昂贵.

For instance, I would like to have a pin be red when the business is closed, and green when the business is open. Maybe even a dollar signs inside of the pin to tell the user how expensive it is.

编辑

我创建了一个名为CustomPin的类,该类采用了MKAnnotation协议.另外,我不希望有一个可以更改的自定义MKAnnotationView图像.这是否意味着我必须在数组中为MKAnnotationView添加多个图像,并且每次有关业务的详细信息更改时,它都要更改图像?

I have created a class called CustomPin that adopts the MKAnnotation protocol. Additionally, I'm not looking to have a custom MKAnnotationView image that can change. Does that mean I'll have to add multiple images for the MKAnnotationView in an array and have it change the image everytime the details about the business change?

先谢谢您!

推荐答案

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

You can create custom MKAnnotationView by simply inheritance. You can use this class to create annotation view from delegate method.

这里是一个例子.

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
    }
}

这篇关于在Swift中创建动态MKAnnotationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆