如何使地图图钉动画掉落? Swift 2.2 IOS 9

How To make Map Pins Animate drop? Swift 2.2 IOS 9

我在此处对我的视图中的地图进行了基本设置,我正在尝试使图钉动画化...就像您在地图应用程序中按住不放,图钉会掉落到该位置一样.所以当我进入视图时,所有的图钉都会动画化到它们的位置,我真的不知道该怎么做。如果有人可以指导我找到我需要添加的正确代码,那就太好了!

这是我的当前代码:

   class FirstViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{



@IBOutlet weak var mapView: MKMapView!





    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad() 

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        self.mapView.showsUserLocation = true 


   let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
        // Drop a pin
        let Litzman = MKPointAnnotation()
        Litzman.coordinate = LitzmanLocation
        Litzman.title = "Litzman Bar"
        Litzman.subtitle = "Nemal Tel Aviv St'"
        mapView.addAnnotation(Litzman)}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    //Dispose of resources that can be re created.

        }

    //Mark:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

    {
        let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))

        self.mapView.setRegion(region, animated: true)

        self.locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print("Errors: " + error.localizedDescription)
    }     

首先,我没有维护任何标准,我只有一个解决你问题的方法,我正在学习 swiftmapKit 通过解决其他问题。

这里我给出了一些示例代码,我通过对象库添加了mapViewUITapGestureRecognizer

用于添加注释,如从顶部删除

pinView!.animatesDrop = true

viewForAnnotation委托中method.AndaddAnnotation方法会在你点击时调用添加注解 在地图上。

用于动画化所有注释

我已经实现了 regionDidChangeAnimated 委托方法,只需删除并重新添加所有注释。

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController,  MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")
    self.mapView.delegate = self
    self.locationManager.delegate = self
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true

    let locationData = [
        //Walker Art Gallery
        ["name": "Walker Art Gallery",
            "latitude": 12,
            "longitude": 79],
        //Liver Buildings
        ["name": "Liver Buildings",
            "latitude": 13,
            "longitude": 77],
        //St George's Hall
        ["name": "St George's Hall",
            "latitude": 14,
            "longitude": 77]
    ]
    var annotations = [MKPointAnnotation]()
    for each in locationData {
        let latitude = CLLocationDegrees(each["latitude"] as! Double)
        let longitude = CLLocationDegrees(each["longitude"] as! Double)
        let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let name = each["name"] as! String
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "\(name)"
        annotations.append(annotation)
    }
    mapView.addAnnotations(annotations)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    print("viewForAnnotation \(annotation.title)")
    if annotation is MKUserLocation {
        return nil
    }
    let reuseID = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
    if(pinView == nil) {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
    }
    return pinView
}

@IBAction func addAnnotation(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        let location = sender.locationInView(mapView)
        let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        mapView.addAnnotation(annotation)
    }
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    print("regionDidChangeAnimated")
    let annotations = self.mapView.annotations
    self.mapView.removeAnnotations(annotations)
    self.mapView.addAnnotations(annotations)
} 
}

编辑

对于你的情况,在 viewDidLoad 方法中添加这个

self.mapView.delegate = self

要注释 MKAnnotation,您必须实现以下委托方法

 //Annotation view
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    let reuseID = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
    if(pinView == nil) {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
    }
    return pinView
}

在注释完所有注释后,您必须在委托方法下实现。

 func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let annotations = self.mapView.annotations
    self.mapView.removeAnnotations(annotations)
    self.mapView.addAnnotations(annotations)
}

而且我假设您知道如何在地图上点击添加注释...

EDIT2

我的朋友,这是你需要的。

在viewDidLoad中添加一个notification observer如下。

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)

最后将 regionDidChangeAnimated 更改为 didBecomeActive,如下所示

func didBecomeActive() {
    let annotations = self.mapView.annotations
    self.mapView.removeAnnotations(annotations)
    self.mapView.addAnnotations(annotations)
}

在Objective C中:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
    // Try to dequeue an existing pin view first.
    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
    if (!pinView)
    {
        // If an existing pin view was not available, create one.
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
        pinView.pinTintColor = [UIColor blueColor];
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}
return nil;
}