2016-08-13 5 views
1

私はmapViewにregionDidChangeAnimatedを使用しています。私はユーザーのズームやドラッグが地図のときに検出したい。ドラッグが検出されていますが、ユーザーがズームしたときに検出できないようです。私は使用しようとしました:スウィフトでズームの効果をスクロールで検出する方法

func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    print("region will change") 

} 

しかし、私はマップをズームすると動作しません。

全コード:

var locationManager: CLLocationManager = CLLocationManager() 
var startLocation: CLLocation! 

@IBOutlet weak var mapView: MKMapView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    //get current location 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 

    self.mapView.showsUserLocation = true 
    startLocation = nil 

    let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:") 
    mapDragRecognizer.delegate = self 
    self.mapView.addGestureRecognizer(mapDragRecognizer) 



    self.mapView.pitchEnabled = true 
    self.mapView.showsBuildings = true 

} 
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.05, longitudeDelta: 0.05)) 
    mapView.setRegion(region, animated: false) 
    let currentLocX = String(format: "%.4f", location!.coordinate.latitude) 
    let currentLocY = String(format: "%.4f", location!.coordinate.longitude) 

    self.locationManager.stopUpdatingLocation() 
} 
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

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


func didDragMap(gestureRecognizer: UIGestureRecognizer) { 

    print("Drag") 
} 
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    print("Zoom") 

} 

答えて

5

私はそれを解決しました。 override func viewDidLoad()を追加する必要がありますself.mapView.delegate = self ズームを決定するための関数を地図に追加してください:

関連する問題