I have a map app that I am making in swift . The app has a custom annotationView where I add an animation to it. The map is coded in my viewController.swift while the animation is created in my CustomAnnotationView.swift file. The animation and map run great. The problem is I have a custom Transition Animation that allows me to add and remove views when segueing through the app. But whenever I segue back to the main view(the map) all my animation stop, they also stop whenever I leave the app and comeback. So what I need help with is figuring out how to keep the animation going whenever I come back to the map.
Heres my code in the viewController.swift
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
// This example is only concerned with point annotations.
guard annotation is CustomPointAnnotation else {
return nil
}
// For better performance, always try to reuse existing annotations. To use multiple different annotation views, change the reuse identifier for each.
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "CustomMarker") {
annotationView.layer.removeAllAnimations()
return annotationView
} else {
//Handles the custom Annoation View
print("the Wave is \(self.EventWave)")
return CustomAnnotationView(reuseIdentifier: "CustomMarker", size: 13.5, wave: self.EventWave)
}
}
Heres my code in the CustomAnnotationView.swift
class CustomAnnotationView: MGLAnnotationView {
var BackView: UIView!
var BackSize: CGFloat!
func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
return CGRect(x: x, y: y, width: width, height: height)
}
init(reuseIdentifier: String, size: CGFloat, wave: CGFloat) {
super.init(reuseIdentifier: reuseIdentifier)
// This property prevents the annotation from changing size when the map is tilted.
scalesWithViewingDistance = false
// Begin setting up the view.
frame = CGRect(x: 0, y: 0, width: size, height: size)
backgroundColor = .green
// Use CALayer’s corner radius to turn this view into a circle.
layer.cornerRadius = size / 2
layer.borderWidth = 1
layer.borderColor = UIColor.white.cgColor
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.1
self.BackSize = size * 2
self.BackView = UIView(frame: CGRect(x: 0, y: 0, width: size, height: size))
self.BackView.layer.cornerRadius = size / 2
self.BackView.backgroundColor = UIColor.green
self.BackView.alpha = 0.3
self.addSubview(self.BackView)
let scaleAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.duration = 0.8
scaleAnimation.repeatCount = 1000000000
scaleAnimation.autoreverses = true
scaleAnimation.fromValue = wave
scaleAnimation.toValue = wave / 2
self.BackView.layer.add(scaleAnimation, forKey: "scale")
}