I am trying to implement a custom slider as shown in figure below.

what I have done so far looks something like this

please help me out for drawing the arc with such effect. my code is as below, what I am doing is drawing the arc using CGContextAddArc with line width kLineWidth.
- (void)drawThumbAtPoint:(CGPoint)sliderButtonCenterPoint inContext: (CGContextRef)context {
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, sliderButtonCenterPoint.x, sliderButtonCenterPoint.y);
CGImageRef imageRef = [UIImage imageNamed:@"circle25.png"].CGImage;
CGRect rect = CGRectMake(sliderButtonCenterPoint.x - kThumbRadius, sliderButtonCenterPoint.y - kThumbRadius, kThumbRadius*2, kThumbRadius*2);
CGContextDrawImage(context, rect, imageRef);
//CGContextAddArc(context, sliderButtonCenterPoint.x, sliderButtonCenterPoint.y, kThumbRadius, 0.0, 2*M_PI, NO);
CGContextFillPath(context);
UIGraphicsPopContext();
}
- (CGPoint)drawArcTrack:(float)track atPoint:(CGPoint)center withRadius:(CGFloat)radius inContext:(CGContextRef)context {
UIGraphicsPushContext(context);
CGContextBeginPath(context);
float angleFromTrack = translateValueFromSourceIntervalToDestinationInterval(track, self.minimumValue, self.maximumValue, 0, M_PI/3);// 2*M_PI
CGFloat startAngle = (4*M_PI)/3;
CGFloat endAngle = startAngle + angleFromTrack;
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, NO);
CGPoint arcEndPoint = CGContextGetPathCurrentPoint(context);
CGContextStrokePath(context);
UIGraphicsPopContext();
return arcEndPoint;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint middlePoint;
middlePoint.x = self.bounds.origin.x + self.bounds.size.width/2;
middlePoint.y = self.bounds.origin.y + self.bounds.size.width;
CGContextSetLineWidth(context, kLineWidth);
CGFloat radius = [self sliderRadius];
[self.maximumTrackTintColor setStroke];
[self drawArcTrack:self.maximumValue atPoint:middlePoint withRadius:radius inContext:context];
[self.minimumTrackTintColor setStroke];
self.thumbCenterPoint = [self drawArcTrack:self.value atPoint:middlePoint withRadius:radius inContext:context];
[self.thumbTintColor setFill];
[self drawThumbAtPoint:self.thumbCenterPoint inContext:context];
}
Unless you are going to be changing the shape dynamically, you would probably be better off just creating the image in an image editor. I know it’s easy to create that effect in Photoshop, Illustrator, or Fireworks.
That said, drawing an inner shadow like that with Core Graphics requires several steps:
CGContextCliporCGContextClipToMask).CGContextSetShadowWithColor).If you do all of that correctly, you can get a nice result like this:
Here’s the code I wrote to draw that. I wrote it in the
drawRect:of a custom view subclass, but you can easily use this code to draw into any graphics context.First, I create a path that’s just an arc:
Next, I ask Core Graphics to make a new path that is the outline of the
arcpath. Note how I ask it for a stroke width ofkArcThicknessand round line caps:I also need the inverse of that path: a path that includes every point except the points in
shape. It so happens (although I don’t think it’s documented) thatCGContextCreateCopyByStrokingPathandCGPathAddRectdraw in opposite directions. So if I copyshapeand draw an enormous rectangle around it, the non-zero winding rule means that the new path will be the inverse ofshape:Now I can actually start drawing. First, I’ll fill in the shape with a light gray color:
Next I actually perform the four steps I listed above. I have to save the graphics state so I can undo the clipping and shadow parameters when I’m done.
Step 1: clip to the shape:
Step 2: Well, I did this step already when I created
shapeInverse.Step 3: I set the shadow parameters:
Step 4: I fill the inverse shape from step 2:
Now I restore the graphics state, which specifically restores the clipping path and unsets the shadow parameters.
Finally, I’ll stroke
shapewith a light gray to make the edge crisper:Of course I clean up when I’m done:
For more complex shapes, you can look at my answer here and my answer here.
Here’s all the code together for easy copying: