一旦地图越界,将Scroll目标从GMSMapView更改为ScrollView

时间:2018-01-18 10:34:33

标签: ios swift gmsmapview

我在scrollView中有一个GMSMapView。我将边界设置为mapView。 有没有办法使从GMSMapView到ScrollView的滚动过渡顺利和正确?

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        var latitude  = position.target.latitude;
        var longitude = position.target.longitude;

        //northEast out of bounds Latitude (top out of bounds)
        if (position.target.latitude > northEastLatitude) {
            scrollView.setContentOffset(CGPoint(x: 0, y: -100), animated: true)
            latitude = northEastLatitude;

        }

        //southWest out of bounds Latitude (bottom out of bounds)
        if (position.target.latitude < southWestLatitude) {
            scrollView.setContentOffset(CGPoint(x: 0, y: 100), animated: true)
            latitude = southWestLatitude;

        }

        //northEast out of bounds Longitude (right out of bounds)
        if (position.target.longitude > nortEastLongitude) {
            longitude = nortEastLongitude;

        }

        //southWest out of bounds Longitude (left out of bounds)
        if (position.target.longitude < southWestLongitude) {
            longitude = southWestLongitude;
        }

        if (latitude != position.target.latitude || longitude != position.target.longitude) {
            var l = CLLocationCoordinate2D();
            l.latitude  = latitude;
            l.longitude = longitude;
            mapView.animate(toLocation: l);
        }
    }

setContentOffset正在运行,但看起来并不是很好。 如果滚动顶部超出界限或机器人超出范围,滚动应继续在ScrollView上。

0 个答案:

没有答案