UIBezierPath 是制作可移动圆角矩形的最佳方法吗?

发布于 2024-12-15 18:36:23 字数 387 浏览 4 评论 0原文

我正在从头开始制作 UISlider。我首先制作一个圆角矩形,我使用下面的代码完成:

CGRect frame = CGRectMake(10, 10, self.frame.size.width, 10);

UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:10.0];
[[UIColor blueColor] setFill];
[path fill];

我看到了一些其他选项来制作圆角矩形,但认为这是最快的方法。使用 UIBezierPath 制作有什么限制吗?也就是说,滑块需要能够根据触摸事件移动,因此我想更改 BezierPath 的 center 属性。这可能吗?

I'm making a UISlider from scratch. I started by making a rounded rectangle, which I did using the code below:

CGRect frame = CGRectMake(10, 10, self.frame.size.width, 10);

UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:10.0];
[[UIColor blueColor] setFill];
[path fill];

I saw some other options to make a rounded rectangle but thought this was the quickest way. Are there any limitations with making one using UIBezierPath? Namely, the slider needs to be able to move upon touch events, so I want to change the center property of a BezierPath. Is this possible?

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

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

发布评论

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

评论(1

北笙凉宸 2024-12-22 18:36:23

每次需要更改滑块位置时,您都需要重新创建贝塞尔曲线路径,或者使用 CGContext 的变换矩阵将其绘制在不同的位置。

我建议您考虑使用 CALayer 作为滑块的移动部分。在 view.layer 中绘制滑块的通道,并添加一个子图层,在其中绘制滑块的“拇指”。然后,当您需要移动拇指层时,只需重新定位它即可。

You would need to either recreate the bezier path each time you need to change the slider position, or use CGContext's transform matrix to draw it in a different place.

I suggest you look at using a CALayer for the moving part of the slider. Draw the channel of the slider in view.layer, and add a sublayer in which you draw the "thumb" of the slider. Then you can just reposition the thumb layer when you need to move it.

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