动态更改带有UI滑块的MKCircle Radius

时间:2014-09-04 17:11:56

标签: ios mkmapview mkannotation

我正在使用MapView处理一个项目。你可以通过Tab放下一个引脚并按住。选择引脚时,将出现一个具有一定半径的圆。但是现在用户应该决定圆圈的半径。这应该可以通过UISlider实现。

问题是,圆半径不会动态变化。圆圈出现,你移动滑块,没有任何反应。取消选择并再次选择引脚,将添加另一个带有新UISlider值的环。

当前代码: 在.h文件中:

@property (retain, nonatomic) MKCircle *pinCircle;
在.m文件中

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    _radius.hidden= false;

    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[(MKPointAnnotation*)[view annotation] coordinate].latitude longitude:[(MKPointAnnotation*)[view annotation] coordinate].longitude];
    _pinCircle = [MKCircle circleWithCenterCoordinate:pinLocation.coordinate radius:_radius.value*1000];
    [_mapView addOverlay:_pinCircle];
    [_pinCircle autorelease];

}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
    _radius.hidden= true;
    }

- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
    MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
    circleView.strokeColor = [UIColor greenColor];
    circleView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.4];
    return [circleView autorelease];
}

我知道我必须使用这个动作:

- (IBAction)radiusSliderChange:(id)sender {
    NSLog(@"Slidervalue changed");
    [self.pinCircle removeFromSuperview];
}

但是这会导致以下错误:

-[MKCircle removeFromSuperview]: unrecognized selector sent to instance 0x18b919d0

根据亚历杭德罗·卡雷拉的帖子,我得到了以下错误:

https://www.dropbox.com/s/saxhns992jex606/Bildschirmfoto%202014-09-04%20um%2020.20.21.png?dl=0

3 个答案:

答案 0 :(得分:2)

你必须在init(pinCircle = nil;)设置为nil然后你必须检查:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
   if(pinCircle != nil) // click two annotations
   {
      [_mapView removeOverlay: _pinCircle];
       _pinCircle = nil;
   }
   _radius = 100; // start value to define
   [self createCircle: view.annotation.coordinate];
}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
    if(pinCircle != nil)
    {
       [_mapView removeOverlay: _pinCircle];
       _pinCircle = nil;
    }
}

- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
    MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
    circleView.strokeColor = [UIColor greenColor];
    circleView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.4];
    return [circleView autorelease];
}

你已经使用了Storyboard,所以你必须有像pinCircle一样的滑块变量

- (IBAction)radiusSliderChange:(id)sender {
    _radius = _slider.value;  // radius property of circle is readonly so you must create other one
    [_mapView removeOverlay: _pinCircle];
    [self createCircle:_pinCircle.coordinate];
}

- (void)createCircle:(CLLocationCoordinate)coord
{
    _pinCircle = [MKCircle circleWithCenterCoordinate:coord radius:_radius.value*1000];
    [_mapView addOverlay:_pinCircle];
    [_pinCircle autorelease];
}

答案 1 :(得分:1)

您可以在.h文件中添加MKCircle * pinCircle,然后像“self.pinCircle”一样访问它。之后,您将能够使用[self.pinCircle removeFromSuperview];将其删除..: - )

答案 2 :(得分:1)

滑块值更改后,您需要删除以前添加的叠加层。

  - (IBAction)sliderForJixRadiusValueChanged:(id)sender {
    //    self.mapSelectorManager.circleRadius = self.sliderForJixRadius.value;

        [_mapView removeOverlays:arrOverlay];

        MKCircle *circle= [MKCircle circleWithCenterCoordinate:_droppedAt1 radius: (_sliderForJixRadius.value)*10000];
        [_mapView addOverlay:circle];
        [arrOverlay addObject:circle];