我有一个快速制作的地图应用程序。该应用程序有一个自定义annotationView,我在其中添加动画。在我的viewController.swift中编码地图,同时在CustomAnnotationView.swift文件中创建动画。动画和地图运行得很好。问题是我有一个自定义的Transition Animation,允许我在通过应用程序进行切换时添加和删除视图。但每当我回到主视图(地图)时,我的所有动画都会停止,只要我离开应用程序并回归,它们也会停止。所以我需要帮助的是弄清楚每当我回到地图时如何保持动画。
继承我在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)
}
}
继续我在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")
}