swift - 如何动画 MKMapKit 注释删除

标签 swift mapkit mapkitannotation

请有人帮忙解决一个 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 秒内滑动到新位置然后消失。实际发生的是它立即移动到新的位置,然后滑回原来的位置,然后消失。我做错了什么?

最佳答案

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
    }
}

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

关于swift - 如何动画 MKMapKit 注释删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53174068/

相关文章:

ios - 返回数组的 Swift 方法

ios - 如何将 Collection View 滚动链接到 mapkit 注释

swift - MKMapView 上的模糊

ios - Mapkit选择所有注释

ios - 随机唯一字符串生成用作随机数(oauth)

json - iOS - 快速下载 json

ios - 在 Swift 4 中自动调整缩放以适应 iOS 中的所有标记

ios - 如何在 IOS 中使用 mapkit 在 map 上显示用户

ios - 如何将 UIButtons 添加到我的 TableViewCell 中?

swift - MapKit .PLIST DICTIONARY with INT and STRING-values = multiple annotations WITH Title & Subtitle from plist (根据 : MapKit Sample WWDC 2017 - 237)