feat: Add background animation in realtime alert

This commit is contained in:
Andrea Busi
2023-07-14 16:59:58 +02:00
parent e531088c86
commit aed78e44cd
2 changed files with 62 additions and 2 deletions
@@ -10,6 +10,52 @@ import UIKit
import MapKit
class RealtimeAlertContainerView: UIView {
lazy var alertView: RealtimeAlertView = {
let view = RealtimeAlertView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var animationColor: UIColor = .white
// MARK: - Init
convenience init() {
self.init(frame: .zero)
configureUI()
}
// MARK: - Private
private func configureUI() {
backgroundColor = .white
addSubview(alertView)
let margin: CGFloat = 24
alertView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: margin).isActive = true
alertView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -margin).isActive = true
alertView.topAnchor.constraint(equalTo: topAnchor, constant: margin).isActive = true
alertView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -margin).isActive = true
}
// MARK: - Public
func startBackgroundAnimation() {
let animation = CABasicAnimation(keyPath: "backgroundColor")
animation.fromValue = UIColor.white.cgColor
animation.toValue = animationColor.cgColor
animation.duration = 2.0
animation.beginTime = CACurrentMediaTime() + 1
animation.autoreverses = true
animation.repeatCount = .infinity
layer.add(animation, forKey: "backgroundColor")
}
}
class RealtimeAlertView: UIView {
let titleLabel: UILabel = {
@@ -46,6 +92,7 @@ class RealtimeAlertView: UIView {
label.font = .preferredFont(forTextStyle: .title3)
label.textColor = AppTheme.Colors.red
label.textAlignment = .center
label.numberOfLines = 2
return label
}()
@@ -16,7 +16,10 @@ class RealtimeAlertViewController: UIViewController, MKMapViewDelegate {
// MARK: - Internal
private let notificationView = RealtimeAlertView()
private let containerView = RealtimeAlertContainerView()
private var notificationView: RealtimeAlertView {
containerView.alertView
}
/// Alert to display
private let realtimeAlert: EQNRealtimePushNotification
/// Timer to constantly update countdown label
@@ -52,7 +55,7 @@ class RealtimeAlertViewController: UIViewController, MKMapViewDelegate {
// MARK: - View Lifecycle
override func loadView() {
view = notificationView
view = containerView
}
override func viewDidLoad() {
@@ -65,10 +68,20 @@ class RealtimeAlertViewController: UIViewController, MKMapViewDelegate {
startWaveAnimation()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
containerView.startBackgroundAnimation()
}
// MARK: - Private
private func configureUI() {
notificationView.closeButton.addTarget(self, action: #selector(onTapClose(_:)), for: .touchUpInside)
// configure color for animation
let intensity = realtimeAlert.relativeIntensity()
containerView.animationColor = intensityTextColor(for: intensity)
}
private func updateUI() {