问题
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.
EDIT
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?
Thank you in advance!
回答1:
You can create custom MKAnnotationView
by simply inheritance. You can use this class to create annotation view from delegate method.
Here is one example.
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
}
}
回答2:
You have to do this programmatically. Following code may help you: Initialize the pin and give it a type, such as opened, closed, expensive etc.
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.type = @"opened"; // <-- set type in annotation
[self.mapView addAnnotation:newAnnotation];
In your (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation method put a simple check
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;
}
来源:https://stackoverflow.com/questions/35691701/creating-a-dynamic-mkannotationview-in-swift