-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorPinAnnotationView.swift
78 lines (56 loc) · 2.14 KB
/
ColorPinAnnotationView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// ColorPinAnnotationView.swift
// ColorPinAnnotationView
//
// Created by Daniel Saidi on 2015-08-18.
// Copyright (c) 2015 Daniel Saidi. All rights reserved.
//
/*
If you use this view instead of the MKPinAnnotationView
class, you can set any UIColor as the pinColor, instead
of the three MKPinAnnotationViewColor values.
Other than that, this class looks a little different if
compared to the default MKPinAnnotationView. It lacks a
shadow and has a cleaner pin hole.
*/
import UIKit
import MapKit
import CoreGraphics
public class ColorPinAnnotationView: MKAnnotationView {
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
update()
}
public override init(frame: CGRect) {
super.init(frame: frame)
update()
}
public override init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
update()
}
public var pinColor = UIColor.redColor() {
didSet { update() }
}
public var animatesDrop = true
func animateDropWithDuration(duration: Double) {
let viewFrame = frame
frame = CGRectOffset(viewFrame, 0, -500)
UIView.animateWithDuration(duration, animations: { self.frame = viewFrame })
}
private func update() {
canShowCallout = true
for view in subviews {
view.removeFromSuperview()
}
let bundle = NSBundle(forClass: object_getClass(ColorPinAnnotationView))
let body = UIImage(named: "ColorPinBody", inBundle: bundle, compatibleWithTraitCollection: nil)
let head = UIImage(named: "ColorPinHead", inBundle: bundle, compatibleWithTraitCollection: nil)
let coloredHead = head!.imageByTintingWithColor(pinColor, andBlendMode: kCGBlendModeScreen)
let offset = 0.5 * body!.size.height - 2
centerOffset = CGPoint(x: 0, y: -offset)
image = body
let headView = UIImageView(image: coloredHead)
addSubview(headView)
}
}