如何在地图上垂直移动(通过Pan Gesture)另一个视图,使得引脚保持在覆盖图上方(而不是实际的MapKit覆盖图),如何将引脚置于地图中心.
请参阅附加屏幕截图,了解第一个和最终状态.
当用户平移上/下时,我已经获得了叠加层和屏幕顶部之间空间的CGRect.然而,当用户向上平移时,我如何使用它来移动地图和图钉同时放大地图……当用户向下平移时再次缩小,到目前为止我已经躲过了.
我尝试过不同的方法,从尝试调整可见矩形到调整地图视图的框架.答案可能在于一些MKMapRect / Region技巧.
(Freepik CC BY 3.0手形图标)
解决方法
实际上,keithbhunter的代码很慢,因为除了更快地更新区域之外,地图也会改变高度,这会导致额外的开销!
我更新了代码,使其顺利运行.
使用此代码,我所做的是保持地图视图的大小相同,但我移动中心点以补偿滑动视图的高度.
要使此代码生效,您必须修改keithbhunter的设置,以便mapView的底部约束完全固定到superview的底部(而不是pullView(以便mapView始终与超级视图的大小相同).设置是一样的.
此外,还可以使用变量maxMetersDistance自定义缩放量
在这里,我总是以埃菲尔铁塔为中心
import UIKit import MapKit class ViewController: UIViewController { @IBOutlet weak var mapView: MKMapView! @IBOutlet weak var slidingView: UIView! @IBOutlet weak var slidingViewHeight: NSLayoutConstraint! var maxMetersDistance:CGFloat = 10000.0; // customize this to set how far the map zooms out of the interest area override func viewDidLoad() { super.viewDidLoad() let pan = UIPanGestureRecognizer(target: self,action: "viewDidPan:") self.slidingView.addGestureRecognizer(pan) firstTimeCenter() } func firstTimeCenter(){ var coordinate = CLLocationCoordinate2D(latitude: 48.8582,longitude: 2.2945) let region = MKCoordinateRegionMakeWithDistance(coordinate,Double(maxMetersDistance),Double(maxMetersDistance)) self.mapView.setRegion(region,animated: true) } func reloadMap() { let height = CGFloat(self.slidingViewHeight.constant) var regionDistance = (maxMetersDistance / self.view.frame.height) * height regionDistance = maxMetersDistance - regionDistance var coordinate = CLLocationCoordinate2D(latitude: 48.8582,longitude: 2.2945) var mapRect = mapView.visibleMapRect; var metersPerMapPoint = MKMetersPerMapPointAtLatitude(coordinate.latitude); var metersPerPixel = CGFloat(metersPerMapPoint) * CGFloat(mapRect.size.width) / CGFloat(mapView.bounds.size.width); var totalMeters = Double(metersPerPixel) * Double(height/2) coordinate = self.translateCoord(coordinate,MetersLat: -totalMeters,MetersLong: 0.0) let region = MKCoordinateRegionMakeWithDistance(coordinate,Double(regionDistance),Double(regionDistance)) self.mapView.setRegion(region,animated: false) } func viewDidPan(panGesture: UIPanGestureRecognizer) { let location = panGesture.locationInView(self.view) self.slidingViewHeight.constant = self.view.frame.size.height - location.y self.reloadMap() } func translateCoord(coord:CLLocationCoordinate2D,MetersLat:Double,MetersLong:Double)->CLLocationCoordinate2D{ var tempCoord = CLLocationCoordinate2D() var tempRegion = MKCoordinateRegionMakeWithDistance(coord,MetersLat,MetersLong); var tempSpan = tempRegion.span; tempCoord.latitude = coord.latitude + tempSpan.latitudeDelta; tempCoord.longitude = coord.longitude + tempSpan.longitudeDelta; return tempCoord; } }