UIBezierPath 是制作可移动圆角矩形的最佳方法吗?
我正在从头开始制作 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次需要更改滑块位置时,您都需要重新创建贝塞尔曲线路径,或者使用 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.