我正在尝试使用 Xcode 6.3 和 swift 制作一个 iOS 应用程序。我使用 MKMapView 来跟踪用户位置。问题是,如果我滚动 map ,我会立即返回到用户位置。这是我的代码:
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
}
如果我滚动 map ,1 秒后我会返回到用户位置。我应该更改 setRegion 方法位置吗?
最佳答案
您需要检测何时滚动 map ,可能通过实现 MKMapViewDelegate
中定义的 mapView(_:regionWillChangeAnimated:)
方法来检测。在此方法中,您需要将 map View 的 userTrackingMode
属性设置为 .None
。当用户平移或缩放 theMap
变量时,将调用您的实现。因此,您应该努力使实现尽可能轻量,因为单个平移或缩放手势可能会多次调用它。
func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) {
if you want to stop tracking the user {
mapView.userTrackingMode = .None
}
}
当您想要再次开始跟踪用户的位置时,请将该属性更改回 .Follow
或 .FollowWithHeading
:
enum 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
}
关于ios - 无法滚动 MKMapView。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31009009/