2016-07-26 4 views
1

私は私のビューでは、マップの基本的な設定を持って、私はピンをアニメーションドロップするようにしようとしている...ちょうど&を押して、ロケーション。だから私がビューに入ると、すべてのピンがその位置へのドロップをアニメートし、私は実際にそれを行う方法を知らない。誰かが私を正しいコードに導くことができるなら、それを追加する必要があります。マップピンをアニメートする方法は?スウィフト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) 
    }  
+0

以下のようがdidBecomeActiveをregionDidChangeAnimatedことを最後に変更 - そうそれはあなたを助けるのが難しいだろう – AlBlue

+0

@AIBlueあなたはどこですか? –

答えて

5

すべて私がどんな水準を維持していないと私は自分の問題の解決策の一つを持っていると私はで迅速mapKitを勉強する前に:ここで

は、私が持っている現在のコードです他の問題を解決する。ここで

私はいくつかのサンプルコードを与えていると私は、オブジェクトライブラリをのMapViewUITapGestureRecognizerを追加しました。 viewForAnnotation代表団に

pinView!.animatesDrop = true 

トップから落下するように注釈を追加するための

addAnnotation方法は、注釈を追加するために呼び出しますmethod.Andときにマップ上タップ。私は実装した

すべての注釈をアニメーション化するため

デリゲートメソッドを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メソッドで、あなたのケースのためにEDIT

あなたがした後、以下のデリゲートメソッドに

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

を実装する必要がMKAnnotationに注釈を付けるために、この

self.mapView.delegate = self 

を追加すべてのアノテーションにアノテーションを付けるあなたはデリゲートメソッドの下で実装する必要があります。

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) 

そして、あなたは、あなたが求めているもので、より具体的に、そしてあなたのコードが正しくフォーマットされていることを確認する必要があり

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

最初に、あなたの時間を助けてくれてありがとう:)、第二に、もし私が私のコードを完全に変更したいだけで、すべての私の注釈がアニメーション化されるという機能を追加したいのですが、 :}再利用ID =ピンとしましょう。 To:PinViewを返します。私のコードでは? –

+0

それは私のために働いていません......あなただけpinViewを実装しようとすることができます!anmiatesDrop =本当に私のコードに変更することなく、それをしないでください? –

+0

私の答えの編集部分を参照してくださいあなたのコードだけを使ってテストしました... –

関連する問題