Quartz 2D 路径旋转

发布于 2024-08-12 04:56:39 字数 1145 浏览 1 评论 0原文

我对 Quartz 2D 还很陌生。

想象一下以下场景:

您有一个圆形的迷你地图视图。 我正在地图顶部绘制三角形(圆弧现在并不重要)。该形状代表可见区域。

当用户改变方向时,我需要让三角形沿着迷你地图旋转。

目前,路径的绘制方式如下:

CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);

CGPath visibleAreaPath = CGPathCreateMutable();

CGPathMoveToPoint(visibleAreaPath, &transform, miniMapCenter.x, miniMapCenter.y);
CGPathAddLineToPoint(visibleAreaPath, &transform, 18.0, 8.0);
CGPathAddLineToPoint(visibleAreaPath, &transform, 66.0, 8.0);

CGPathCloseSubpath(visibleAreaPath);

然后,我使用 CAShapeLayer 绘制路径,如下所示:

CALayer *rootLayer = [visibleAreaView layer];

visibleAreaShape = [CAShapeLayer layer];
[visibleAreaShape setFillColor:[UIColor colorWithHue:0.584 saturation:0.8 brightness:0.9 alpha:0.6].CGColor];
[visibleAreaShape setFillRule:kCAFillRuleNonZero];
[visibleAreaShape setAnchorPoint:CGPointMake(0.5, 0.5)];
[rootLayer addSublayer:visibleAreaShape];
[visibleAreaShape setPath:visibleAreaPath];

路径正在旋转,但不基于给定的原点。 请记住,设置图层的锚点对我没有帮助,因为我想要的是旋转路径(最终我什至不需要显示它,因为我将使用它来确定哪些点在迷你上可见)地图)。

关于如何实现这一目标有什么想法吗? 谢谢。

I'm fairly new to Quartz 2D.

Imagine the following scenario:

You have a circle-shaped mini map view.
I'm drawing triangle (the arc isn't important right now) on top of the map. This shape represents the visible area.

I need to have the triangle shape rotate along the mini map as the user changes orientation.

Currently this how the path is being drawn:

CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);

CGPath visibleAreaPath = CGPathCreateMutable();

CGPathMoveToPoint(visibleAreaPath, &transform, miniMapCenter.x, miniMapCenter.y);
CGPathAddLineToPoint(visibleAreaPath, &transform, 18.0, 8.0);
CGPathAddLineToPoint(visibleAreaPath, &transform, 66.0, 8.0);

CGPathCloseSubpath(visibleAreaPath);

I then draw the path using a CAShapeLayer like so:

CALayer *rootLayer = [visibleAreaView layer];

visibleAreaShape = [CAShapeLayer layer];
[visibleAreaShape setFillColor:[UIColor colorWithHue:0.584 saturation:0.8 brightness:0.9 alpha:0.6].CGColor];
[visibleAreaShape setFillRule:kCAFillRuleNonZero];
[visibleAreaShape setAnchorPoint:CGPointMake(0.5, 0.5)];
[rootLayer addSublayer:visibleAreaShape];
[visibleAreaShape setPath:visibleAreaPath];

The path is being rotated, but not based on a given origin.
Keep in mind that setting the layer's anchor point doesn't help me since what I want is to rotate the path (ultimately I wouldn't even need to display it, since I will be using it to determine which points are visible on the mini map).

Any ideas on how to accomplish this?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

枕梦 2024-08-19 04:56:39

我建议您以极坐标形式存储点,并在需要时转换为路径。在极坐标中旋转非常容易(只需更改 theta 值)。

I would suggest you store the points in polar form and convert to a path when needed. It's very easy to rotate in polar coordinates (simply change the theta value).

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