【问题标题】:regiondidchange called several times on app load swiftregiondidchange 在应用程序加载迅速时调用了几次
【发布时间】:2015-10-14 16:51:09
【问题描述】:

我有一张地图,当我平移或键入一个位置并导航到它时,我希望运行一个简单的打印功能。但是,我已经完成了这项工作,当我第一次加载应用程序时,它会多次调用该打印函数,直到完成放大。有没有办法不计算应用程序加载区域更改的初始值?

【问题讨论】:

  • 我试过 func mapViewDidFinishLoadingMap(mapView: MKMapView){ func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { hobo() }}
  • 还有这个 func mapViewDidFinishRenderingMap(mapView: MKMapView, fullyRendered: Bool) { func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { hobo() }}

标签: ios xcode swift mapkit swift2


【解决方案1】:

答案终于在这里找到了http://ask.ttwait.com/que/5556977

private var mapChangedFromUserInteraction = false

private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view = self.mapView.subviews[0]
//  Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers {
    for recognizer in gestureRecognizers {
        if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
            return true
        }
    }
}
return false
}

func mapView(mapView: MKMapView, regionWillChangeAnimated animated:    
Bool) {
mapChangedFromUserInteraction =   
mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
    // user changed map region
}
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated:   
Bool) {
if (mapChangedFromUserInteraction) {
    // user changed map region
}
}

【讨论】:

    【解决方案2】:

    这是更简洁的 Swift 4.2 答案。

    private var mapChangedFromUserInteraction = false
    
    public func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        if (mapChangedFromUserInteraction) {
           // do something
        }
    }
    
    private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
        return map.subviews.first?.gestureRecognizers?
            .contains(where: {
                $0.state == .began || $0.state == .ended
            }) == true
    }
    
    public func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
        if !mapChangedFromUserInteraction { // I only wanted to check until this was true
            mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2021-04-03
      相关资源
      最近更新 更多