【发布时间】:2014-12-03 22:09:27
【问题描述】:
我已经研究过这个问题,很遗憾我没有得到任何针对这个问题的解决方案。
问题是这样的:
我有一个覆盖了 drawRect 的自定义 MKAnnotationView。我不能使用 image 属性,因为我有一个名为 dotColor 的属性,它会影响注释视图的绘制。
代码:
class CustomAnnotationView: MKAnnotationView {
var dotColor: UIColor!
init(annotation: CustomAnnotation, color: UIColor) {
super.init(annotation: annotation, reuseIdentifier: "CustomAnnotationView")
self.annotation = annotation
frame.size = CGSize(width: 20, height: 20)
dotColor = color
opaque = false
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
if annotation != nil && dotColor != nil {
let context = UIGraphicsGetCurrentContext()
let circleFrame = CGRectInset(rect, 2, 2)//Needs the inset to show the stroke completely
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextSetStrokeColorWithColor(context, dotColor.CGColor)
CGContextSetLineWidth(context, 3)
CGContextFillEllipseInRect(context, circleFrame)
CGContextStrokeEllipseInRect(context, circleFrame)
}
}
}
但是当我点击地图上的注释视图时,它没有显示标注(我很确定 MKAnnotation 有一个标题。你可以查看这个线程的答案:"If the title is nil, the callout...")
【问题讨论】:
-
如果您能指出代码中的一些错误,我将不胜感激。
-
canShowCallout是否设置为true?在opaque = false之后,尝试做canShowCallout = true。 -
@Anna 谢谢你的回答,因为它有效!你让我开心。
标签: ios objective-c swift map