【问题标题】:How To make Map Pins Animate drop? Swift 2.2 IOS 9如何使地图引脚动画下降?斯威夫特 2.2 IOS 9
【发布时间】:2016-07-26 20:44:19
【问题描述】:

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

这是我的当前代码:

   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)
    }     

【问题讨论】:

  • 您需要更具体地说明您的要求,并确保您的代码格式正确 - 否则将很难为您提供帮助
  • @AIBlue 我的朋友你在哪里?

标签: swift mkmapview ios9 mapkit mkannotation


【解决方案1】:

首先,我没有维护任何标准,我只是为您的问题提供了一种解决方案,我正在通过解决其他问题来学习 swiftma​​pKit。 p>

这里我给出了一些示例代码,并通过 Object 库添加了 ma​​pViewUITapGestureRecognizer

用于添加注释,例如从顶部下降

pinView!.animatesDrop = true

viewForAnnotation委托方法中。当您在地图上点击时,addAnnotation方法将调用以添加注释。

用于动画所有注释

我已经实现了 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 添加一个通知观察者如下。

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)
}

【讨论】:

  • 首先,感谢您抽出宝贵时间提供帮助 :),其次,我想知道我是否不想想完全更改我的代码并添加我所有的功能注释将动画我只是复制此代码来自:}让重用ID = Pin。到:返回 PinView 。在我的代码中?
  • 它对我不起作用......你能尝试只对我的代码实施 pinView.!anmiatesDrop = true 而不改变它吗?
  • 啊,谢谢,我现在更清楚该做什么了。我有一个小假期,将在 2 天内测试这个并报告!
  • 好吧,我测试了它,但每次我在地图中四处走动时它都会下降,而不仅仅是在我打开地图视图时......你知道如何解决这个问题吗?谢谢
【解决方案2】:

在目标 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多