要使用MKAnnotationView创建自定义"pin-drop"动画,您需要遵循以下步骤:
以下是一个简单的示例代码:
import MapKit
class CustomAnnotationView: MKAnnotationView {
var pinImageView: UIImageView!
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
pinImageView = UIImageView(image: UIImage(named: "pin-drop.png"))
addSubview(pinImageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
pinImageView.frame = bounds
}
func animateDrop() {
let scaleUpTransform = CGAffineTransformMakeScale(1.2, 1.2)
let scaleDownTransform = CGAffineTransformMakeScale(1.0, 1.0)
pinImageView.transform = scaleUpTransform
UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseInOut, animations: {
self.pinImageView.transform = scaleDownTransform
}, completion: nil)
}
}
然后,在您的地图代理方法中,您可以使用以下代码来创建自定义的MKAnnotationView并添加到地图上:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseIdentifier = "customAnnotationView"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? CustomAnnotationView
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
annotationView?.animateDrop()
return annotationView
}
这样,当您将自定义的MKAnnotationView添加到地图上时,它将自动执行"pin-drop"动画。
领取专属 10元无门槛券
手把手带您无忧上云