【发布时间】:2011-02-22 17:12:03
【问题描述】:
问题 我正在尝试围绕 annonation 创建一个视觉半径圆,该圆实际上保持固定大小。例如。因此,如果我将半径设置为 100m,当您缩小地图视图时,半径圆会逐渐变小。
我已经能够实现缩放,但是当用户操纵视图时,半径矩形/圆似乎“抖动”远离 Pin Placemark。
我相信这在即将推出的 iPhone OS 4 上更容易实现,但是我的应用程序需要支持 3.0。
显化 这是video 的行为。
实施 注释以通常的方式添加到 Mapview,并且我使用我的 UIViewController 子类 (MapViewController) 上的委托方法来查看区域何时发生变化。
-(void)mapView:(MKMapView *)pMapView regionDidChangeAnimated:(BOOL)animated{
//Get the map view
MKCoordinateRegion region;
CGRect rect;
//Scale the annotations
for( id<MKAnnotation> annotation in [[self mapView] annotations] ){
if( [annotation isKindOfClass: [Location class]] && [annotation conformsToProtocol:@protocol(MKAnnotation)] ){
//Approximately 200 m radius
region.span.latitudeDelta = 0.002f;
region.span.longitudeDelta = 0.002f;
region.center = [annotation coordinate];
rect = [[self mapView] convertRegion:region toRectToView: self.mapView];
if( [[[self mapView] viewForAnnotation: annotation] respondsToSelector:@selector(setRadiusFrame:)] ){
[[[self mapView] viewForAnnotation: annotation] setRadiusFrame:rect];
}
}
}
Annotation 对象 (LocationAnnotationView) 是 MKAnnotationView 的子类,它的 setRadiusFrame 看起来像这样
-(void) setRadiusFrame:(CGRect) rect{
CGPoint centerPoint;
//Invert
centerPoint.x = (rect.size.width/2) * -1;
centerPoint.y = 0 + 55 + ((rect.size.height/2) * -1);
rect.origin = centerPoint;
[self.radiusView setFrame:rect];
}
最后,radiusView 对象是 UIView 的子类,它重写了 drawRect 方法来绘制半透明的圆圈。 setFrame 在这个 UIView 子类中也被覆盖了,但是除了 [UIView setFrame:] 之外,它仅用于调用 [UIView setNeedsDisplay] 以确保在框架更新后重新绘制视图。
radiusView 对象 (CircleView) 的 drawRect 方法如下所示
-(void) drawRect:(CGRect)rect{
//NSLog(@"[CircleView drawRect]");
[self setBackgroundColor:[UIColor clearColor]];
//Declarations
CGContextRef context;
CGMutablePathRef path;
//Assignments
context = UIGraphicsGetCurrentContext();
path = CGPathCreateMutable();
//Alter the rect so the circle isn't cliped
//Calculate the biggest size circle
if( rect.size.height > rect.size.width ){
rect.size.height = rect.size.width;
}
else if( rect.size.height < rect.size.width ){
rect.size.width = rect.size.height;
}
rect.size.height -= 4;
rect.size.width -= 4;
rect.origin.x += 2;
rect.origin.y += 2;
//Create paths
CGPathAddEllipseInRect(path, NULL, rect );
//Create colors
[[self areaColor] setFill];
CGContextAddPath( context, path);
CGContextFillPath( context );
[[self borderColor] setStroke];
CGContextSetLineWidth( context, 2.0f );
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextAddPath(context, path );
CGContextStrokePath( context );
CGPathRelease( path );
//CGContextRestoreGState( context );
}
感谢您对我的支持,感谢您提供任何帮助。 乔纳森
【问题讨论】:
标签: objective-c cocoa-touch uiview mkmapview mkannotationview