MKAnnotationView 在滑动和双击缩放时消失

发布于 2024-08-22 05:17:53 字数 998 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-08-29 05:17:53

你添加这个方法了吗?

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    [self setNeedsDisplay];
}

这是取自 Apple 名为 WeatherMap 的示例代码应用程序,该应用程序已从 Apple 开发人员中心删除,但可以在 github 上找到
https://github.com/acekiller/iOS-Samples /blob/master/WeatherMap/Classes/WeatherAnnotationView.m

Did you add this method?

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    [self setNeedsDisplay];
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文