我现在正在使用 MapView 进行一个项目。您可以通过 Tab 放置图钉并按住。当您选择引脚时,将出现一个具有一定半径的圆。但现在用户应该决定圆的半径。这应该可以通过 UISlider 实现。
问题是,圆的半径不会动态变化。圆圈出现,您移动 slider ,没有任何反应。取消选择并再次选择引脚,将添加具有新 UISlider 值的另一个环。
当前代码: 在 .h 文件中:
@property (retain, nonatomic) MKCircle *pinCircle;
在 .m 文件中:
-(void)mapViewMKMapView *)mapView didSelectAnnotationViewMKAnnotationView *)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)mapViewMKMapView *)mapView didDeselectAnnotationViewMKAnnotationView *)view {
_radius.hidden= true;
}
- (MKOverlayView *)mapViewMKMapView *)map viewForOverlayid <MKOverlay>)overlay
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlayverlay];
circleView.strokeColor = [UIColor greenColor];
circleView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.4];
return [circleView autorelease];
}
我知道我必须使用这个 Action :
- (IBAction)radiusSliderChangeid)sender {
NSLog(@"Slidervalue changed");
[self.pinCircle removeFromSuperview];
}
但这会导致以下错误:
-[MKCircle removeFromSuperview]: unrecognized selector sent to instance 0x18b919d0
根据 Alejandro F. Carrera 的帖子,我收到以下错误:
https://www.dropbox.com/s/saxhns992jex606/Bildschirmfoto%202014-09-04%20um%2020.20.21.png?dl=0
你必须在初始化时设置为 nil (pinCircle = nil;) 然后你必须检查这个:
-(void)mapViewMKMapView *)mapView didSelectAnnotationViewMKAnnotationView *)view
{
if(pinCircle != nil) // click two annotations
{
[_mapView removeOverlay: _pinCircle];
_pinCircle = nil;
}
_radius = 100; // start value to define
[self createCircle: view.annotation.coordinate];
}
-(void)mapViewMKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
if(pinCircle != nil)
{
[_mapView removeOverlay: _pinCircle];
_pinCircle = nil;
}
}
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlayverlay];
circleView.strokeColor = [UIColor greenColor];
circleView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.4];
return [circleView autorelease];
}
你已经为此使用了 Storyboard,所以你必须有像 pinCircle 这样的 slider 变量
- (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];
}
关于ios - 使用 UI slider 动态更改 MKCircle 半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671328/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |