【问题标题】:How to animate an MKMapKit annotation removal如何动画 MKMapKit 注释删除
【发布时间】:2018-11-06 14:36:25
【问题描述】:

请有人帮忙解决 MapView 问题。我有一个自定义注释。当用户点击注释时,我希望它向上移动屏幕然后消失。作为动画代码的测试(因为我是新手!),我尝试了以下方法:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let pinTapped = view.annotation as? Pin else {return}
    guard let pinName = pinTapped.title else { return }
    let endFrame = CGRect(x: view.frame.origin.x, y: view.frame.origin.y - self.view.bounds.size.height, width: view.frame.size.width, height: view.frame.size.height)
    UIView.animate(withDuration: 3.0, animations: {
        view.frame = endFrame
    }) { (finished) in
        self.mapKitView.removeAnnotation(pinTapped)
    }
}

我预计注释会在 3 秒内滑到新位置,然后消失。实际发生的是它立即移动到新位置,然后滑回原来的位置,然后消失。我做错了什么?

【问题讨论】:

  • i.stack.imgur.com/lAuf8.gif 对我来说工作得很好,也许问题出在你代码的其他部分。
  • 我发现我的测试结果很奇怪,因为我使用了允许位置模拟的方案,并且我的位置被设置为旅程。当我尝试使用静态方案时,效果更好。它仍然不完美。例如,我还添加了代码以使注释在移动前几秒钟放大。这不能正常工作,因为当您点击注释时,注释具有内置动画。

标签: swift mapkit mapkitannotation


【解决方案1】:
extension UIView {
    func disappearAnimation(_ completion: @escaping () -> ()) {

        let opacity = CASpringAnimation(keyPath: "opacity")
        let position = CASpringAnimation(keyPath: "position")
        let group = CAAnimationGroup()

        opacity.fromValue = 1
        opacity.toValue = 0

        position.fromValue = self.center
        position.toValue = CGPoint(x: self.center.x, y: self.center.y - 300) //You can change it

        group.animations = [opacity, position]

        group.duration = 1.1 // You can change it
        group.repeatCount = 0
        group.autoreverses = false
        group.timingFunction = CAMediaTimingFunction(name: .easeOut) //You can change also timing func

        layer.add(group, forKey: nil)

        DispatchQueue.main.asyncAfter(deadline: .now()+1) {
            completion()
        }
    }
}

添加此动画并根据需要进行自定义后,

func pinTapped(_ pin: MKAnnotationView) {

    pin.disappearAnimation {
        //Remove your view after animation is shown
    }
}

我希望这个解决方案有所帮助

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 2023-03-12
    相关资源
    最近更新 更多