gpt4 book ai didi

iphone - 限制 MKMapView 滚动

转载 作者:行者123 更新时间:2023-12-03 18:29:52 26 4
gpt4 key购买 nike

我正在尝试将自定义图像作为 MKOverlayView 添加到 MKMapView 中 - 我需要限制用户能够滚动到叠加层的边界之外。是否有现有的功能可以做到这一点?或者还有其他建议吗?

谢谢,马特

最佳答案

如果您只想将 map View 卡住在叠加层上,则可以将 map View 的区域设置为叠加层的边界,并将 scrollEnabledzoomEnabled 设置为

但这不会让用户在叠加层的边界内滚动或缩放。

没有内置方法可以将 map View 限制在叠加层的边界内,因此您必须手动执行此操作。首先,确保您的 MKOverlay 对象实现 boundingMapRect 属性。然后可以在 regionDidChangeAnimated 委托(delegate)方法中使用它来根据需要手动调整 View 。

以下是如何完成此操作的示例。
下面的代码应该位于具有 MKMapView 的类中。
确保 map View 最初设置为叠加层可见的区域。

//add two ivars to the .h...
MKMapRect lastGoodMapRect;
BOOL manuallyChangingMapRect;

//in the .m...
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
if (manuallyChangingMapRect)
return;
lastGoodMapRect = mapView.visibleMapRect;
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if (manuallyChangingMapRect) //prevents possible infinite recursion when we call setVisibleMapRect below
return;

// "theOverlay" below is a reference to your MKOverlay object.
// It could be an ivar or obtained from mapView.overlays array.

BOOL mapContainsOverlay = MKMapRectContainsRect(mapView.visibleMapRect, theOverlay.boundingMapRect);

if (mapContainsOverlay)
{
// The overlay is entirely inside the map view but adjust if user is zoomed out too much...
double widthRatio = theOverlay.boundingMapRect.size.width / mapView.visibleMapRect.size.width;
double heightRatio = theOverlay.boundingMapRect.size.height / mapView.visibleMapRect.size.height;
if ((widthRatio < 0.6) || (heightRatio < 0.6)) //adjust ratios as needed
{
manuallyChangingMapRect = YES;
[mapView setVisibleMapRect:theOverlay.boundingMapRect animated:YES];
manuallyChangingMapRect = NO;
}
}
else
if (![theOverlay intersectsMapRect:mapView.visibleMapRect])
{
// Overlay is no longer visible in the map view.
// Reset to last "good" map rect...
[mapView setVisibleMapRect:lastGoodMapRect animated:YES];
}
}

我尝试使用内置的 MKCircle 叠加层进行此操作,似乎效果很好。

<小时/>

编辑:

它确实在 95% 的情况下工作良好,但是,我通过一些测试证实它可能会在两个位置之间振荡,然后进入无限循环。所以,我编辑了一下,我认为这应该可以解决问题:

// You can safely delete this method:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {

}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
// prevents possible infinite recursion when we call setVisibleMapRect below
if (manuallyChangingMapRect) {
return;
}

// "theOverlay" below is a reference to your MKOverlay object.
// It could be an ivar or obtained from mapView.overlays array.

BOOL mapContainsOverlay = MKMapRectContainsRect(mapView.visibleMapRect, theOverlay.boundingMapRect);

if (mapContainsOverlay) {
// The overlay is entirely inside the map view but adjust if user is zoomed out too much...
double widthRatio = theOverlay.boundingMapRect.size.width / mapView.visibleMapRect.size.width;
double heightRatio = theOverlay.boundingMapRect.size.height / mapView.visibleMapRect.size.height;
// adjust ratios as needed
if ((widthRatio < 0.6) || (heightRatio < 0.6)) {
manuallyChangingMapRect = YES;
[mapView setVisibleMapRect:theOverlay.boundingMapRect animated:YES];
manuallyChangingMapRect = NO;
}
} else if (![theOverlay intersectsMapRect:mapView.visibleMapRect]) {
// Overlay is no longer visible in the map view.
// Reset to last "good" map rect...
manuallyChangingMapRect = YES;
[mapView setVisibleMapRect:lastGoodMapRect animated:YES];
manuallyChangingMapRect = NO;
} else {
lastGoodMapRect = mapView.visibleMapRect;
}
}

以防万一有人正在寻找快速的 MKOverlay 解决方案,这里有一个:

- (void)viewDidLoad {
[super viewDidLoad];

MKCircle* circleOverlay = [MKCircle circleWithMapRect:istanbulRect];
[_mapView addOverlay:circleOverlay];

theOverlay = circleOverlay;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
MKCircleView* circleOverlay = [[MKCircleView alloc] initWithCircle:overlay];
[circleOverlay setStrokeColor:[UIColor mainColor]];
[circleOverlay setLineWidth:4.f];

return circleOverlay;
}

关于iphone - 限制 MKMapView 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com