【问题标题】:Stopping all animations in place on MKMapView Swift 3 iOS在 MKMapView Swift 3 iOS 上停止所有动画
【发布时间】:2017-05-20 18:34:06
【问题描述】:

过去一天我一直被这个问题困扰。我创建了一个自定义 MKAnnotation 子类来在 MKMapView 上显示各种自定义引脚。我递归地调用一个函数,使这些图钉在地图上保持动画。我的目标是在用户点击按钮时停止所有这些动画。我试过了

self.view.layer.removeAllAnimations()

self.map.layer.removeAllAnimations()

和其他 hacky 解决方案,但似乎没有一个有效。

以下是创建动画/引脚移动的代码

func animate(duration:Double, newLocation:CLLocationCoordinate2D){
    UIView.animate(withDuration: duration, animations: {
        self.coordinate = newLocation
    }) { (done) in
        self.finished_segment()
    }
} 

非常感谢任何建议。

【问题讨论】:

  • 您或许可以使用 iOS 10 的 UIViewPropertyAnimator 执行此操作。为了看看这是否可行,我将为每个 UIView 设置一个动画师,然后在每个动画师上调用 .startAnimation() 来启动动画。要停止特定UIView 的动画,请在相应的动画师上调用.stopAnimation(true)

标签: ios swift animation


【解决方案1】:

对于任何陷入此问题的人。问题是我必须从与注释关联的 MKAnnotationView 中删除动画。我基本上在我在 mapView 注释委托方法中设置的自定义类中创建了一个成员变量,如下所示。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView") ?? MKAnnotationView()
    if let a = annotation as? Alien{
        annotationView.image = a.image
        a.annotationView = annotationView
    }

    return annotationView
}

并从地图中删除动画。只需调用

self.annotationView.layer.removeAllAnimations()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多