MKOverlay有时会消失

8
我在我的地图中有两个叠加选项:MKCircleOverlay和MKPolygonOverlay。第一个是通过UISlider控制的可变半径。后者可以根据角落的数量和位置进行自定义。如果我快速更改圆的半径(减小UISlider的值),有时我的叠加层会消失(圆形),然后无法再绘制多边形(当然也包括圆形)。应用程序没有崩溃。可能是什么原因呢?
这是我使用的一些代码:
- (IBAction) addCircle:(id)sender
{
slider.hidden = NO;
slider.transform = CGAffineTransformMakeRotation(M_PI*(-0.5));

_longPressRecognizer= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
_longPressRecognizer.minimumPressDuration = 1.0; 

[mapview addGestureRecognizer:_longPressRecognizer];
[_longPressRecognizer release];
}

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
    return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapview];    
CLLocationCoordinate2D touchMapCoordinate = [mapview convertPoint:touchPoint toCoordinateFromView:mapview];

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Circle Based Search";

[mapview addAnnotation:pa];
[pa release];

tmC = touchMapCoordinate;
double radius = 1000.0;

self.circleOverlay = [MKCircle circleWithCenterCoordinate:tmC radius:radius];
[mapview removeOverlays:[mapview overlays]];
[mapview addOverlay:circleOverlay];
[mapview removeAnnotations:[mapview annotations]];
}


-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
if ([overlay isKindOfClass:[MKCircle class]])
{
    MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay] ;
    circleView.fillColor = [UIColor blueColor];
    circleView.strokeColor = [UIColor blueColor];
    circleView.lineWidth = 5.0;
    circleView.alpha = 0.20;
        return circleView;
}
else
    if ([overlay isKindOfClass:[MKPolygon class]])
{
    MKPolygonView *polygonView = [[MKPolygonView alloc] initWithOverlay:overlay ];
    polygonView.fillColor = [UIColor blueColor];
    polygonView.strokeColor = [UIColor blueColor];
    polygonView.lineWidth = 5.0;
    polygonView.alpha = 0.20;
    return polygonView;
}
return [kml viewForOverlay:overlay];
}

- (void)addCircleWithRadius:(double)radius
{
self.circleOverlay = [MKCircle circleWithCenterCoordinate:tmC radius:radius];
[mapview removeOverlays:[mapview overlays]];
[mapview addOverlay:circleOverlay];
[mapview removeAnnotations:[mapview annotations]];
}

- (IBAction)sliderChanged:(UISlider *)sender
{
    double radius = (sender.value);
[self addCircleWithRadius:radius];
[mapview removeAnnotations:[mapview annotations]];
}

请展示您用于调整圆形大小的代码。在我们看到代码之前,我们只能猜测可能发生了什么。 - sosborn
刚刚完成了,非常抱歉,我完全忘记添加代码了。 - Hari
你应该在sliderChanged方法中使用NSLog输出半径值,以确保它返回的是你期望的值。同时,也要记录tmc值,以确保中心点停留在你期望的位置上。不确定代码结构,但当你缓慢移动滑块时,可能会触发长按手势识别器。 - sosborn
实际上,尽管没有NSlogs,但我看到它的功能正常,圆圈的大小正在按照预期更改,并且中心可见且未改变。 - Hari
[mapview addOverlay:circleOverlay]; 应该改为 [mapview addOverlay:self.circleOverlay]; - med200
显示剩余3条评论
1个回答

3

问题解决了。是与 UISlider 相关的问题,在nib文件中,通过slider的属性检查器,我们发现在Values下勾选了Continuous属性。取消该勾选后,问题得以解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接