如何将 MKAnnotation Pin 一个一个丢掉?
Posted
技术标签:
【中文标题】如何将 MKAnnotation Pin 一个一个丢掉?【英文标题】:How to drop MKAnnotation Pin one by one? 【发布时间】:2017-07-31 11:46:24 【问题描述】:需要将注释一一放入地图...甚至从左到右放置注释针。有没有办法让注解drop更加时尚简洁
func addannotaiton()
sistermaplist.mapType = MKMapType.standard
sistermaplist.removeAnnotations(sistermaplist.annotations)
var i = 0
let count = mapsisterarray.count
let span: MKCoordinateSpan = MKCoordinateSpanMake(1.5,1.5)
while (i != count )
let mbr = mapsisterarray[i]
if mbr.lat != "",mbr.lon != ""
let latitude = Double((mbr.lat ?? "0.0")!)
let longitude = Double((mbr.lon ?? "0.0")!)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!)
let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
sistermaplist.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = mbr.name ?? ""
annotation.subtitle = mbr.meet_place ?? ""
sistermaplist.addAnnotation(annotation)
i += 1
【问题讨论】:
删除一个 MKAnnotation pin 是什么意思?在英语中,这听起来更像是您想删除一个。然而,您的代码表明您正在添加一个。 注解通常以动画的形式拖放到地图上......这就是我所说的丢弃注解 【参考方案1】:如果你想控制动画,我建议你自己做。您可以将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 Pin 一个一个丢掉?的主要内容,如果未能解决你的问题,请参考以下文章
iPhone:MKAnnotation Pin 颜色“选定状态”