无法滚动MKMapView。

时间:2015-06-23 17:00:45

标签: ios swift

我正在尝试使用Xcode 6.3和swift来创建iOS应用程序。我使用MKMapView来跟踪用户位置。问题是,如果我滚动地图,我会立即返回到用户位置。这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    manager = CLLocationManager()
    manager.delegate = self      
    manager.desiredAccuracy = kCLLocationAccuracyBest   
    manager.requestAlwaysAuthorization()                
    manager.startUpdatingLocation()                     

    theMap.delegate = self
    theMap.mapType = MKMapType.Standard
    theMap.zoomEnabled = true          
    theMap.addGestureRecognizer(longPress)
    theMap.scrollEnabled = true

}

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

    let spanX = 0.007
    let spanY = 0.007
    var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
    theMap.setRegion(newRegion, animated: false)
    theMap.scrollEnabled = true

}

如果我滚动地图,1秒后我返回到用户位置。我应该更改setRegion方法的位置吗?

1 个答案:

答案 0 :(得分:0)

您需要检测滚动地图的时间,可能是通过实施MKMapViewDelegate中定义的userTrackingMode方法。在此方法中,您需要将地图视图的.None属性设置为theMap。当用户平移或缩放func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) { if you want to stop tracking the user { mapView.userTrackingMode = .None } } 变量时,将调用您的实现。因此,您应该尽可能地将实现保持为轻量级,因为对于单个平移或缩放手势可以多次调用它。

.Follow

如果您想再次开始关注用户的位置,请将该属性更改回.FollowWithHeadingenum MKUserTrackingMode : Int { case None // the user's location is not followed case Follow // the map follows the user's location case FollowWithHeading // the map follows the user's location and heading }

{{1}}