refactor: Rework image creation

This commit is contained in:
Andrea Busi
2022-11-17 22:13:18 +01:00
parent 95a214403b
commit 3e122240dc
4 changed files with 28 additions and 19 deletions
@@ -119,7 +119,7 @@
EQNMapAnnotationUserReport *report = (EQNMapAnnotationUserReport *)annotation;
EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.SmallIdentifier];
annotationView.image = report.image;
annotationView.image = [report imageWithHeight:EQNCustomAnnotationView.SmallViewImageHeight];
annotationView.title = report.timeDifference;
return annotationView;
}
@@ -342,7 +342,8 @@ class SegnalazioniMapViewController: EQNBaseMapViewController {
let identifier = showAnnotationTime ? EQNCustomAnnotationView.SingleLineIdentifier : EQNCustomAnnotationView.SmallIdentifier
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
let size = showAnnotationTime ? EQNCustomAnnotationView.SingleLineImageHeight : EQNCustomAnnotationView.SmallViewImageHeight
annotationView.image = annotation.image(with: size)
annotationView.title = annotation.timeDifference
annotationView.canShowCallout = true
// Psizioniamo più in alto le segnalazioni con intensità maggiore.
@@ -17,9 +17,9 @@ class EQNMapAnnotationUserReport: NSObject, MKAnnotation {
@objc var coordinate: CLLocationCoordinate2D
@objc var title: String?
@objc var subtitle: String?
@objc var image: UIImage?
@objc var timeDifference: String?
private let magnitude: Int
var report: EQNSegnalazione?
// MARK: - Init
@@ -37,21 +37,24 @@ class EQNMapAnnotationUserReport: NSObject, MKAnnotation {
magnitude: Int,
coordinate: CLLocationCoordinate2D
) {
self.magnitude = magnitude
self.coordinate = coordinate
self.title = Self.title(for: magnitude)
self.subtitle = Self.subtitle(for: magnitude)
self.coordinate = coordinate
self.image = Self.image(for: magnitude)
}
// MARK: - Helper
private static func image(for magnitute: Int) -> UIImage? {
guard let color = UIColor(named: "Mercalli \(magnitute)") else {
print("[EQNMapAnnotationUserReport] Unable to get a color for magnitude: \(magnitute)")
@objc(imageWithHeight:)
func image(
with height: CGFloat
) -> UIImage? {
guard let color = UIColor(named: "Mercalli \(magnitude)") else {
print("[EQNMapAnnotationUserReport] Unable to get a color for magnitude: \(magnitude)")
return nil
}
return UIImage.circle(diameter: EQNCustomAnnotationView.SmallHeight, color: color, borderWidth: 1.0, borderColor: .darkGray)
return UIImage.circle(diameter: height, color: color, borderWidth: 1.0, borderColor: .black)
}
private static func title(for magnitude: Int) -> String {
@@ -17,9 +17,14 @@ public class EQNCustomAnnotationView: MKAnnotationView {
@objc static let SingleLineIdentifier = "EQNCustomAnnotationViewSingleLine"
@objc static let DoubleLineIdentifier = "EQNCustomAnnotationViewDoubleLine"
static let SmallHeight: CGFloat = 12.0
private static let HeightLabel: CGFloat = 15.0
@objc static let SmallViewHeight: CGFloat = 12.0
@objc static let SmallViewImageHeight: CGFloat = SmallViewHeight
@objc static let SingleLineViewHeight: CGFloat = 40.0
@objc static let SingleLineLabelHeight: CGFloat = 15.0
@objc static let SingleLineImageHeight: CGFloat = SingleLineViewHeight - SingleLineLabelHeight
@objc static let DoubleLineViewHeight: CGFloat = 55.0
@objc static let DoubleLineLabelHeight: CGFloat = 15.0
@objc static let DoubleLineImageHeight: CGFloat = DoubleLineViewHeight - 2*DoubleLineLabelHeight
// MARK: - Public
@@ -70,13 +75,13 @@ public class EQNCustomAnnotationView: MKAnnotationView {
backgroundColor = .clear
if reuseIdentifier == Self.SmallIdentifier {
frame = CGRect(x: 0, y: 0, width: Self.SmallHeight, height: Self.SmallHeight)
frame = CGRect(x: 0, y: 0, width: Self.SmallViewHeight, height: Self.SmallViewHeight)
setupSmallUI()
} else if reuseIdentifier == Self.SingleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 40)
frame = CGRect(x: 0, y: 0, width: Self.SingleLineViewHeight, height: Self.SingleLineViewHeight)
setupSingleLineUI()
} else if reuseIdentifier == Self.DoubleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 55)
frame = CGRect(x: 0, y: 0, width: 40, height: Self.DoubleLineViewHeight)
setupDoubleLineUI()
}
}
@@ -93,7 +98,7 @@ public class EQNCustomAnnotationView: MKAnnotationView {
}
private func setupSingleLineUI() {
let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel)
let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.SingleLineLabelHeight)
labelTop.frame = labelFrame
addSubview(labelTop)
@@ -101,12 +106,12 @@ public class EQNCustomAnnotationView: MKAnnotationView {
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
let yOffeset = frame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0)
let yOffeset = frame.height / 2.0 - (Self.SingleLineLabelHeight + 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)
let labelTopFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.DoubleLineLabelHeight)
labelTop.frame = labelTopFrame
addSubview(labelTop)
@@ -114,7 +119,7 @@ public class EQNCustomAnnotationView: MKAnnotationView {
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)
labelBottom.frame = CGRect(x: 0, y: imageView.frame.maxY, width: frame.width, height: Self.DoubleLineLabelHeight)
addSubview(labelBottom)
}
}