0

進学先の大学を表示するアプリを作ろうとしていますが、1都市しか表示できず困っています。ユーザーが都市を過ぎてスクロールできないようにしようとしています。次に、その領域をオーバーレイしようとしています。setRegion メソッドがその問題の修正に役立つと思いましたが、明らかにそうではありません。ユーザーが超えられない地域を設定する方法について何か提案はありますか?

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    // sets maps to univeristy
    var location = CLLocationCoordinate2DMake(42.9633,
        -85.890042)
    // Span and region
    var span = MKCoordinateSpanMake (0.005, 0.005)
    var region = MKCoordinateRegion(center: location, span: span)
    Map.setRegion(region, animated: true)
4

1 に答える 1

1

ここにある Obj-C コードを翻訳しました: https://gist.github.com/Alp-Phone/e11cca67e77285566d4dから Swift にリンクは死んでいます。

lazy var restrictedRegion: MKCoordinateRegion = {
    // sets maps to univeristy
    let location = CLLocationCoordinate2DMake(42.9633, -85.890042)
    // Span and region
    let span = MKCoordinateSpanMake (0.005, 0.005)
    return MKCoordinateRegion(center: location, span: span)
}()

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.setRegion(restrictedRegion, animated: true)
}

var manuallyChangingMap = false //Stop from updating while animating
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if !manuallyChangingMap && ((mapView.region.span.latitudeDelta > restrictedRegion.span.latitudeDelta * 4) ||
            (mapView.region.span.longitudeDelta > restrictedRegion.span.longitudeDelta * 4) ||
            fabs(mapView.region.center.latitude - restrictedRegion.center.latitude) > restrictedRegion.span.latitudeDelta ||
            fabs(mapView.region.center.longitude - restrictedRegion.center.longitude) > restrictedRegion.span.longitudeDelta) {

        manuallyChangingMap = true
        mapView.setRegion(restrictedRegion, animated: true)
        manuallyChangingMap = false
    }
}
于 2015-08-04T02:14:18.873 に答える