0

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")
}
Markinson
  • 857
  • 1
  • 7
  • 13
  • Why don't you just implement the viewWillAppear(_:) and viewWillDissapear(_:) methods and handle stop / start animations there? https://developer.apple.com/reference/uikit/uiviewcontroller/1621510-viewwillappear https://developer.apple.com/reference/uikit/uiviewcontroller/1621485-viewwilldisappear , you can do this by example: http://stackoverflow.com/questions/14131345/ios-refresh-annotations-on-mapview (as noted in the comments on the thread you can just change the coord of the annotation to refresh it) –  Feb 12 '17 at 22:59
  • @Sneak but how would I do that? especially with my code being in two different files?? – Markinson Feb 15 '17 at 22:02
  • I don't understand what you mean? What is the problem with adding viewWillAppear and dissapear as mentioned? –  Feb 15 '17 at 22:06
  • @Sneak where would I add it? in my viewController.swift (where the map is) or my CustomAnnontationView.swift (where the animation is)? – Markinson Feb 16 '17 at 00:22
  • "When I segue to the mainView" , I guess thats where your animation lies (viewController), and thats where you call it. But it depends on how your UI looks, otherwise just change the coordination on the annotation when you go back to your mainView and they will refresh the animation as mentioned in the link above. –  Feb 16 '17 at 08:01
  • @Sneak I can't change my coordinates though. They are being pulled down from firebase. And as far as my question I feel like you think I'm talking about something else. My animation is programmed in my CustomAnnotationView.swift not in my ViewController.swift. So I don't see how I can start or stop it it from there since that is not where the original code for it is. Unless theres something I'm missing? – Markinson Feb 17 '17 at 01:39
  • 1
    The coordinates can be changed with unnoticable precision, however, the second option as mentioned in the link is to remove and re-add the annoations when your viewController (mapview) appears again as I mentioned, and your animations will start animating again, but this takes more performance, however, depending on how many annotations you have, you can do whichever fits the need best. When you change coordinates **or** remvoe-readd annoations your dequeueReusableAnnotationView will be called again and start your animations again. –  Feb 17 '17 at 02:25
  • @Sneak okay I see how I could go about removing and adding the annotations again. But if I wanted to take the route about the coordinates how would that code look? – Markinson Feb 17 '17 at 11:53
  • I havn't implemented it myself, since there is no method for doing it you probably need to create a custom method for it: http://stackoverflow.com/questions/9563137/moving-annotations-in-mkmapview-according-to-server-data-latitude-and-longitude , otherwise you go by this http://stackoverflow.com/questions/27969937/moving-annotation-from-one-coordinate-point-to-another-in-swift , but you should try to remove/re-add **First** and see how the performance works out for you, usually it shouldnt be a problem since the dequeue method takes care of the reuse for you etc. –  Feb 17 '17 at 14:09

0 Answers0