2017-02-22 14 views
-1

によってクリックされたときのMapViewコールアウト注釈を可視化(下底)リンクされた画像を観察してくださいスウィフト - ユーザは、ピンが画面の端付近に終わる場所に地図画面を移動すると、ピン注釈がユーザ

とそのピン注釈を選択すると、Appleメソッドは、ピン注釈の吹き出しが表示されるように、マップ画面を画面の中央に向かって少し移動します。

私の質問は2つあり:アップルの方法は、その画面の調整を行うために

I.と呼ばれていますか?

II。 MKMapViewDelegate拡張機能が実装されている場合、mapView didSelectビューメソッド内で同じ機能を実装する最も賢い方法は何でしょうか?

のMapView画面調整シーケンス:

答えて

0

理論的には、あなたは注釈ビューのフレームを取得し、それはマップビューのエッジに近すぎるのですかどうかを確認することができます

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    // if view.frame is too close to the edges of the MKMapView, triggered your desired method 
} 

実際には、アノテーションビューのコールアウトバブルにも依存するため、これは信頼性がありません。コールアウトバブルのサイズに応じて、「端に近すぎる」基準には異なるしきい値が必要です。簡単な方法はありません。

これは少しハッキリです:画面が調整されたら、regionWillChangeAnimatedメソッドが呼び出されます。それは、注釈を叩くのスプリット秒以内にトリガされた場合は、注釈をタップし、ユーザーによって引き起こされたことを良いチャンスがあります:

weak var tappedAnnotationView: MKAnnotationView? 

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    self.tappedAnnotationView = view // this property will only be non-nil for 0.1 seconds 
    DispatchQueue.global().asyncAfter(deadline: .now() + 0.1) { 
     self.tappedAnnotationView = nil 
    } 
} 

func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    if self.tappedAnnotationView != nil { 
     print("map adjusted due to tap on an annotation view") 
    } 
} 

それは私がそれを投げた基本的なテストのために動作しますが、明らかにそこにそれがハックだから故障する極端な場合になります。私が思い付いた何

+0

お返事いただきありがとうございます。それは有り難いです。 –

0

は次のとおりです。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 

    // Get Coordinate for Selected Map Pin 
    let selectedPinCoordinate = (view.annotation?.coordinate)! 

    // Determine (X,Y) coordinates for Selected Map Pin on view (mapView) 
    let SelectedPinMapPoint = mapView.convert(selectedPinCoordinate, toPointTo: mapView) 

    // Determine (X,Y) coordinate for center point of view (mapView) 
    let mapCenterMapPoint = mapView.convert(mapView.centerCoordinate, toPointTo: mapView) 

    // Define an inner view within the existing view frame. Any Selected Map Pin outside 
    // bounds of inner view is deemed to be too close to the edge of the map for the callout 
    // to be fully viewable by the user 
    let innerRectFrameMinX = mapView.frame.maxX * 0.15 
    let innerRectFrameMaxX = mapView.frame.maxX * 0.85 
    let innerRectFrameMinY = mapView.frame.maxY * 0.30 
    let innerRectFrameMaxY = mapView.frame.maxY * 0.85 

    // Create a variable which will serve as the new center (X,Y) coordinate 
    var newCenter = CGPoint() 

    // Default new center (X,Y) coordinate to current view (X,Y) coordinate 
    newCenter.x = mapCenterMapPoint.x 
    newCenter.y = mapCenterMapPoint.y 


    // Determine if x coordinate of Selected Map Pin is outside bounds. If so, 
    // set a new center x coordinate so callout can be clearly seen by the user 
    switch (SelectedPinMapPoint.x) { 

    case _ where (SelectedPinMapPoint.x < innerRectFrameMinX): 
     newCenter.x = mapView.frame.midX - (innerRectFrameMinX - SelectedPinMapPoint.x) 

    case _ where (SelectedPinMapPoint.x > innerRectFrameMaxX): 
     newCenter.x = mapView.frame.midX + (SelectedPinMapPoint.x - innerRectFrameMaxX) 

    default: break 

    } 

    // Determine if y coordinate of Selected Map Pin is outside bounds. If so, 
    // set a new center y coordinate so callout can be clearly seen by the user 
    switch (SelectedPinMapPoint.y) { 

    case _ where (SelectedPinMapPoint.y < innerRectFrameMinY): 
     newCenter.y = mapView.frame.midY - (innerRectFrameMinY - SelectedPinMapPoint.y) 

    case _ where (SelectedPinMapPoint.y > innerRectFrameMaxY): 
     newCenter.y = mapView.frame.midY + (SelectedPinMapPoint.y - innerRectFrameMaxY) 

    default: break 

    } 

    // Convert new map Center (X,Y) coordinate to map coordinate 
    let newCenterCoordinate = mapView.convert(newCenter, toCoordinateFrom: nil) 

    // Set new center as center for map view 
    mapView.setCenter(newCenterCoordinate, animated: true) 

} 

私はそれが提供するどのような利益のために私の構想のラフスケッチグラフィックをリンクされてきました。 Rough Graphical Sketch

関連する問題