refactor: Edit custom annotation view to support a 2 labels layout

This commit is contained in:
Andrea Busi
2021-03-15 16:39:28 +01:00
parent b5525eda01
commit 2cbb4cd526
5 changed files with 58 additions and 25 deletions
@@ -40,7 +40,7 @@
{
[super viewDidLoad];
[self.mapView registerClass:[EQNCustomAnnotationView class] forAnnotationViewWithReuseIdentifier:EQNCustomAnnotationView.Identifier];
[self.mapView registerClass:[EQNCustomAnnotationView class] forAnnotationViewWithReuseIdentifier:EQNCustomAnnotationView.SingleLineIdentifier];
}
- (void)didReceiveNotification:(UNNotification *)notification
@@ -109,7 +109,7 @@
if ([annotation isKindOfClass:[EQNMapAnnotationPastquake class]]) {
EQNMapAnnotationPastquake *pastquake = (EQNMapAnnotationPastquake *)annotation;
EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.Identifier];
EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.SingleLineIdentifier];
annotationView.image = pastquake.image;
annotationView.title = pastquake.title;
return annotationView;
@@ -117,7 +117,7 @@
} else if ([annotation isKindOfClass:[EQNMapAnnotationUserReport class]]) {
EQNMapAnnotationUserReport *report = (EQNMapAnnotationUserReport *)annotation;
EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.Identifier];
EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.SingleLineIdentifier];
annotationView.image = report.image;
annotationView.title = report.title;
return annotationView;
@@ -35,7 +35,7 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa
mapView.delegate = self
mapView.isScrollEnabled = false
mapView.isZoomEnabled = false
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier)
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier)
}
}
private var impactTimestamp: Date?
@@ -112,7 +112,7 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa
return nil
}
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
annotationView.title = annotation.title
return annotationView
@@ -33,7 +33,7 @@ class PasquakesMapViewController: EQNBaseMapViewController {
// MARK: - Public
override func registerMapAnnotationViews() {
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier)
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier)
}
override func loadDataSource() {
@@ -142,7 +142,7 @@ class PasquakesMapViewController: EQNBaseMapViewController {
return nil
}
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
annotationView.title = annotation.title
@@ -30,7 +30,7 @@ class SegnalazioniMapViewController: EQNBaseMapViewController {
// MARK: - Public
override func registerMapAnnotationViews() {
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier)
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier)
}
override func loadDataSource() {
@@ -270,7 +270,7 @@ class SegnalazioniMapViewController: EQNBaseMapViewController {
return nil
}
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
annotationView.title = annotation.title
@@ -12,9 +12,10 @@ import MapKit
@objc
public class EQNCustomAnnotationView: MKAnnotationView {
@objc static let Identifier = "EQNCustomAnnotationView"
private static let AnnotationFrame = CGRect(x: 0, y: 0, width: 40, height: 40)
@objc static let SingleLineIdentifier = "EQNCustomAnnotationViewSingleLine"
@objc static let DoubleLineIdentifier = "EQNCustomAnnotationViewDoubleLine"
private static let HeightLabel: CGFloat = 15.0
@@ -26,8 +27,13 @@ public class EQNCustomAnnotationView: MKAnnotationView {
}
@objc public var title: String? {
set { label.text = newValue }
get { label.text }
set { labelTop.text = newValue }
get { labelTop.text }
}
@objc public var subtitle: String? {
set { labelBottom.text = newValue }
get { labelBottom.text }
}
// MARK: - UI
@@ -38,40 +44,67 @@ public class EQNCustomAnnotationView: MKAnnotationView {
return imageView
}()
private lazy var label: UILabel = {
private lazy var labelTop: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
label.font = UIFont.systemFont(ofSize: 11, weight: .medium)
label.textAlignment = .center
return label
}()
private lazy var labelBottom: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 11, weight: .medium)
label.textAlignment = .center
label.textColor = .red
return label
}()
// MARK: - Init
@objc
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
frame = Self.AnnotationFrame
backgroundColor = .clear
setupUI()
if reuseIdentifier == Self.SingleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 40)
setupUI()
} else if reuseIdentifier == Self.DoubleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 55)
setupDoubleLineUI()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Private
private func setupUI() {
backgroundColor = .clear
let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel)
labelTop.frame = labelFrame
addSubview(labelTop)
let labelFrame = CGRect(x: 0, y: 0, width: Self.AnnotationFrame.width, height: Self.HeightLabel)
label.frame = labelFrame
addSubview(label)
let imageViewHeight = Self.AnnotationFrame.height - labelFrame.height
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: Self.AnnotationFrame.width, height: imageViewHeight)
let imageViewHeight = frame.height - labelFrame.height
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
let yOffeset = Self.AnnotationFrame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0)
let yOffeset = frame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0)
centerOffset = CGPoint(x: 0, y: yOffeset)
}
private func setupDoubleLineUI() {
let labelTopFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel)
labelTop.frame = labelTopFrame
addSubview(labelTop)
let imageViewHeight = frame.height - labelTopFrame.height * 2.0
imageView.frame = CGRect(x: 0, y: labelTopFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
labelBottom.frame = CGRect(x: 0, y: imageView.frame.maxY, width: frame.width, height: Self.HeightLabel)
addSubview(labelBottom)
}
}