Files
eqn.ios/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift
T
2021-03-14 15:12:16 +01:00

78 lines
2.1 KiB
Swift

//
// EQNCustomAnnotationView.swift
// Earthquake Network
//
// Created by Andrea Busi on 06/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import Foundation
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)
private static let HeightLabel: CGFloat = 15.0
// MARK: - Public
@objc public override var image: UIImage? {
set { imageView.image = newValue }
get { imageView.image }
}
@objc public var title: String? {
set { label.text = newValue }
get { label.text }
}
// MARK: - UI
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var label: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
label.textAlignment = .center
return label
}()
// MARK: - Init
@objc
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
frame = Self.AnnotationFrame
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
backgroundColor = .clear
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)
addSubview(imageView)
let yOffeset = Self.AnnotationFrame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0)
centerOffset = CGPoint(x: 0, y: yOffeset)
}
}