Martin Gunnarsson Martin Gunnarsson - 25 days ago 9x
Swift Question

App crash when trying to animate MKAnnotationView

Following a few examples online, I'm trying to animate an MKAnnotationView. I've placed the following code in the init() of my MKAnnotationView subclass:

UIView.animate(withDuration: 10.0, delay: 0.0, options: .repeat, animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: {
self.frame = biggerFrame

UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: {
self.frame = originalFrame
}, completion: nil)

As soon as the annotation views are added to the map, the app crashes with the following exception:

2016-09-25 19:32:58.655 MyApp 42[62332:4524529] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[UIView addKeyframeWithStartTime:duration:animations:] must be called inside the animations block of +[UIView animateKeyframesWithDuration:delay:options:animations:completion:]'

Any ideas what I'm doing wrong?



As the console suggested, you should use:

UIView.animateKeyframes(withDuration: TimeInterval, delay: TimeInterval, options: UIViewKeyframeAnimationOptions, animations: {
    your animation here
    }, completion: nil)

instead :)

Currently, you are using UIView.animate(withDuration:delay:Option:animations:Completion:)