MKAnnotationView 在滑动和双击缩放时消失
我对 MKAnnotationView 进行了子类化,以创建一个注释,该注释基本上通过重写 drawRect 来围绕地图视图上的点绘制一个圆。在以下情况下(在模拟器中),圆圈可以正常绘制:
- 地图视图初始加载时
- 滑动时,但仅当滑动运动在触摸结束之前停止时(以便触摸结束后地图不会“滑行”)
- 捏合时缩放
当发生以下任何操作时,圆圈将消失:
- 触摸结束后在地图“滑行”处滑动
- 双击缩放 圆圈
消失后,如果执行“工作”组中的任何操作,圆圈将重新出现。
什么可能导致这种情况?我不是绘图/显示/布局专家(坦率地说,我也不是 obj C 或 iPhone 专家)。
下面是一些稍微简化的代码,似乎与我的 MKAnnotationView 子类最相关:
- (void)drawRect:(CGRect)rect {
// Drawing code
[self drawCircleAtPoint:CGPointMake(0,0) withRadius:self.radiusInPixels andColor:self.circleAnnotation.color];
}
- (void)drawCircleAtPoint:(CGPoint)p withRadius:(int)r {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
float alpha = 0.75;
CGContextSetRGBFillColor(contextRef, 255, 0, 0, alpha);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, alpha);
// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(0, 0, 2*r, 2*r));
}
I have subclassed MKAnnotationView to create an annotation that basically draws a circle around a point on a map view through override of drawRect. The circle draws fine in the following situations (in the simulator):
- On initial load of the map view
- On swipe, but only when swipe motion is stopped before touch ends (so that map doesn't "coast" after touch ends)
- On pinch zoom
The circle will disappear when any of the following actions occur:
- Swipe where map "coasts" after touch ends
- Double-tap zoom
The circle will reappear if any of the actions in the "working" group are taken after it has disappeared.
What might cause this? I'm not a draw/display/layout expert (frankly, I'm not an obj C or iPhone expert either).
Here is some slightly simplified code that seems most relevant from my MKAnnotationView subclass:
- (void)drawRect:(CGRect)rect {
// Drawing code
[self drawCircleAtPoint:CGPointMake(0,0) withRadius:self.radiusInPixels andColor:self.circleAnnotation.color];
}
- (void)drawCircleAtPoint:(CGPoint)p withRadius:(int)r {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
float alpha = 0.75;
CGContextSetRGBFillColor(contextRef, 255, 0, 0, alpha);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, alpha);
// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(0, 0, 2*r, 2*r));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你添加这个方法了吗?
这是取自 Apple 名为 WeatherMap 的示例代码应用程序,该应用程序已从 Apple 开发人员中心删除,但可以在 github 上找到
https://github.com/acekiller/iOS-Samples /blob/master/WeatherMap/Classes/WeatherAnnotationView.m
Did you add this method?
This is taken from Apple's sample code app called WeatherMap which was removed from Apple Developer Center, but can be found on github
https://github.com/acekiller/iOS-Samples/blob/master/WeatherMap/Classes/WeatherAnnotationView.m