Ios 如何使用自定义图像设置MKAnnotationView拖放的动画
当我尝试访问MKAnnotationView的animate drop属性时,它不存在。我知道MKPinAnnotationView存在此属性,但我使用的是自定义图像,因此必须使用MKAnnotationView。有没有办法设置MKAnnotationView放置的动画?代码:Ios 如何使用自定义图像设置MKAnnotationView拖放的动画,ios,swift3,mapkit,mkannotation,Ios,Swift3,Mapkit,Mkannotation,当我尝试访问MKAnnotationView的animate drop属性时,它不存在。我知道MKPinAnnotationView存在此属性,但我使用的是自定义图像,因此必须使用MKAnnotationView。有没有办法设置MKAnnotationView放置的动画?代码: func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotati
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier)
annotationView?.canShowCallout = true
let pinImage = UIImage(named:"mappin")!
annotationView?.image = pinImage
//annotationView?.animatesDrop = true
} else {
annotationView?.annotation = annotation
}
return annotationView
}
1)创建一个xib
文件和一个相关的.swift
文件来保存动画注释代码
2)在AnimatedAnnotation.xib
中,您可以发挥UI的创造力,创建注释
3)在AnimatedAnnotation.swift
文件中,创建自己的AnimatedAnnotation
类
class AnimatedAnnotation: MKAnnotationView {
}
4)接下来,编写一个配置方法,该方法将使用animationDuration
和annotationImage
配置此“不可见”容器视图的实例(这将是您的自定义图像)
5)然后声明UIImageView
类型的属性animatedView
,并在配置块中实例化它
class AnimatedAnnotation: UIView {
var animatedView: UIImageView!
var animationDuration: Double!
var annotationImage: UIImage!
func configureWith(animationDuration: Double, annotationImage: UIImage) {
self.backgroundColor = .clear
self.annotationImage = annotationImage
self.animationDuration = animationDuration
instantiateAnimatedView()
}
func instantiateAnimatedView() {
let startingPosition = CGRect(x: 0, y: 0, width: 20, height: 20) // This is whatever starting position you want
let imageView = UIImageView(frame: startingPosition)
imageView.image = UIImage(name: "YourAnnotationImage")
imageView.alpha = 0
addSubview(imageView!)
}
}
6)编写使动画视图显示的方法,并在其“不可见”超级视图上设置动画
class AnimatedAnnotation: UIView {
var animatedView: UIImageView!
var animationDuration: Double!
var annotationImage: UIImage!
func configureWith(animationDuration: Double, annotationImage: UIImage) {
self.backgroundColor = .clear
self.annotationImage = annotationImage
self.animationDuration = animationDuration
instantiateAnimatedView()
dropAnnotation() // Call whenever you want the drop to happen
}
func instantiateAnimatedView() {
let startingPosition = CGRect(x: 0, y: 0, width: 20, height: 20) // This is whatever starting position you want
let imageView = UIImageView(frame: startingPosition)
imageView.image = annotationImage
imageView.alpha = 0
addSubview(imageView!)
}
func dropAnnotation() {
let dropHeight:CGFloat = self.bounds.size.height - animatedView.frame.size.height
let endPosition = CGPoint(x: animatedView.center.x, y: animatedView.center.y + dropHeight)
UIView.animate(withDuration: animationDuration, animations: {
self.animatedView.alpha = 1
self.animatedView.center = endPosition
})
}
}
7)用法在
viewFor annotation:MKAnnotation)->MKAnnotationView?
方法中,实例化AnimatedAnnotation
并在方法中返回该注释
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let bundle = Bundle.main // or your framework bundle
let xib = UINib(nibName: "AnimatedAnnotation", bundle: bundle)
let view = xib.instantiate(withOwner: self, options: nil)[0] as! AnimatedAnnotation
view.configureWith(animationDuration: 0.25, annotationImage: UIImage(named: "Your-Annotation-Image")
return view
}
我实际上没有在Xcode中尝试过这个,我只是在SO编辑器中编写了这个,但是应该可以。让我知道您的进度。我的回答对您有帮助吗?谢谢。我试试这个,让你看看know@Shekar你对我的解决方案有什么经验?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let bundle = Bundle.main // or your framework bundle
let xib = UINib(nibName: "AnimatedAnnotation", bundle: bundle)
let view = xib.instantiate(withOwner: self, options: nil)[0] as! AnimatedAnnotation
view.configureWith(animationDuration: 0.25, annotationImage: UIImage(named: "Your-Annotation-Image")
return view
}