ios - 无法滚动 MKMapView。

标签 ios swift

我正在尝试使用 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/

相关文章:

ios - 使用 CAShapeLayer 作为 CAShapeLayer 蒙版时的细边框

ios - 为什么在Braintree手动集成上出现找不到“Braintree/BTUICTAControl.h”文件的错误?

ios - 加载数据后如何滚动到行?

ios - 如何将带有字符串的自定义日期格式写入其中?

ios - 在 RxSwift 中一次只执行一次 subscribe 方法

ios - TableView 到 PDF 添加填充到页面

ios - UITableView tableFooterView 中的按钮在弹跳后调整 TableView 时不响应触摸

ios - 以编程方式从应用程序获取 iOS 设备日志

ios - 填充 UITableView 的最有效方法

swift - UITabBarController 不加载自定义 UITableView