尝试为 MKAnnotationView 设置动画时应用程序崩溃

App crash when trying to animate MKAnnotationView

根据几个在线示例,我正在尝试为 MKAnnotationView 制作动画。我已将以下代码放入 MKAnnotationView 子类的 init() 中:

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)

将注释视图添加到地图后,应用程序立即崩溃并出现以下异常:

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:]'

知道我做错了什么吗?

谢谢!

按照控制台的建议,您应该使用:

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

改为:)

目前,您正在使用 UIView。animate(withDuration:delay:Option:animations:Completion:)