Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top