如何为MKMapKit注释移除设置动画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为MKMapKit注释移除设置动画相关的知识,希望对你有一定的参考价值。
请有人帮忙解决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
}
}
我希望这个解决方案有所帮
以上是关于如何为MKMapKit注释移除设置动画的主要内容,如果未能解决你的问题,请参考以下文章
如何为 JQuery addClass/removeClass 函数设置动画?
如何为 XSLT 代码片段配置 CruiseControl 的 C# 版本?