本文介绍了UIBezierPath是制作可移动圆角矩形的最佳方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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];

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

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?

推荐答案

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

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.

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

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.

这篇关于UIBezierPath是制作可移动圆角矩形的最佳方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:32