シンプルがあります。 MKAnnotationView
をサブクラス化して、サブビューとしてピンを追加することができます。ピンをアニメートして、いつでも好きなようにドロップすることができます。
これは、ビューにすべての注釈を同時に追加すると言いますが、移動自体をアニメートします。
ほとんどのコードで行われている非常に簡単な例溶液(マップビューとボタンがストーリーボードにある)であるので、あなたはそれを参照として使用することができますが、いくつかの余分な作業が必要になります...
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func dropPinsPressed(_ sender: Any) {
let coordinates: [CLLocationCoordinate2D] = (0..<50).map { _ in
let point = CGPoint(x: CGFloat(Int(arc4random())%1000)/1000.0 * view.bounds.size.width, y: CGFloat(Int(arc4random())%1000)/1000.0 * view.bounds.size.height)
return mapView.convert(point, toCoordinateFrom: mapView)
}
let anotations = coordinates.map { Annotation(coordinate: $0) }
let sorted = anotations.sorted { $0.coordinate.longitude < $1.coordinate.longitude }
let duration: TimeInterval = 2.0 // overall animation duration
let durationFragment = duration/TimeInterval(sorted.count) // Duration between 2 drops
sorted.enumerated().forEach { $1.delay = durationFragment*TimeInterval($0) }
mapView.addAnnotations(sorted)
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return PinView(animateDropFrom: view.frame.size.height, delay: (annotation as? Annotation)?.delay ?? 0.0)
}
}
fileprivate extension ViewController {
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var delay: TimeInterval = 0.0
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
}
fileprivate extension ViewController {
class PinView: MKAnnotationView {
lazy var pinView: UIView = {
let view = UIView(frame: self.bounds)
view.backgroundColor = UIColor.red
self.addSubview(view)
return view
}()
convenience init(animateDropFrom height: CGFloat, delay: TimeInterval) {
self.init(frame: CGRect(x: 0.0, y: 0.0, width: 10.0, height: 20.0))
pinView.frame = CGRect(x: bounds.origin.x, y: bounds.origin.y-height, width: bounds.size.width, height: bounds.size.height)
UIView.animate(withDuration: 0.3, delay: delay, options: [.curveEaseIn], animations: {
self.pinView.frame = self.bounds
}, completion: nil)
}
}
}
後
デザインの次の最大の問題は、現在のマップをスクロールしてから元に戻すと、ピンが再びドロップされることです。 Annotation
はカスタムサブクラスなので、おそらくそのクラスに垂直オフセットを追加するだけです。次に、最初にアニメーションを作成したときにオフセットをゼロにリセットすると、再びアニメーション化されません。これがあなたの期待される行動でない限り...
MKAnnotationピンを削除することはどういう意味ですか?英語では、それを削除したいと思うように聞こえます。しかし、あなたのコードはあなたが1つを追加していることを示唆しています。 –
注釈は、マップするためのドロップとして通常アニメーション化されています。これは、注釈を削除することによって意味したものです –