Files
eqn.ios/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift

126 lines
4.3 KiB
Swift

//
// EQNCustomAnnotationView.swift
// Earthquake Network
//
// Created by Andrea Busi on 06/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import UIKit
import MapKit
@objc
public class EQNCustomAnnotationView: MKAnnotationView {
@objc static let SmallIdentifier = "EQNCustomAnnotationViewSmall"
@objc static let SingleLineIdentifier = "EQNCustomAnnotationViewSingleLine"
@objc static let DoubleLineIdentifier = "EQNCustomAnnotationViewDoubleLine"
@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
@objc public override var image: UIImage? {
set { imageView.image = newValue }
get { imageView.image }
}
@objc public var title: String? {
set { labelTop.text = newValue }
get { labelTop.text }
}
@objc public var subtitle: String? {
set { labelBottom.text = newValue }
get { labelBottom.text }
}
// MARK: - UI
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var labelTop: UILabel = {
let label = UILabel()
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)
backgroundColor = .clear
if reuseIdentifier == Self.SmallIdentifier {
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: Self.SingleLineViewHeight, height: Self.SingleLineViewHeight)
setupSingleLineUI()
} else if reuseIdentifier == Self.DoubleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: Self.DoubleLineViewHeight)
setupDoubleLineUI()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Private
private func setupSmallUI() {
imageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
addSubview(imageView)
}
private func setupSingleLineUI() {
let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.SingleLineLabelHeight)
labelTop.frame = labelFrame
addSubview(labelTop)
let imageViewHeight = frame.height - labelFrame.height
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
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.DoubleLineLabelHeight)
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.DoubleLineLabelHeight)
addSubview(labelBottom)
}
}