Is UIBezierPath the best way to make a movable rounded rectangle?

爱⌒轻易说出口 提交于 2019-12-13 07:01:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8109482/is-uibezierpath-the-best-way-to-make-a-movable-rounded-rectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!