I need to make a curve in a UIView
as displayed in the image below. I have to use UIBezierPath
. Kindly help me out with this.
I also want to know how to flip the curve from the horizontal axis, so that I have the arc at the top and the base at the bottom.
To draw a solid filled in arc within a particular
CGSize
, you can define aUIBezierPath
like so:That's just using a little trigonometry to calculate the angle and radius for the arc given the height and width of the view.
Once you have that, you can either construct a
CAShapeLayer
using that path and then add that as a sublayer of aUIView
or you can implement your owndrawRect
method that callsfill
on that path. (Or, given that you've tagged this with core-graphics, you could also do a customdrawRect
with CoreGraphics calls, but I'm not sure why you'd do that.)For example, you could define a
CurvedView
class that usesCAShapeLayer
:And
That yields:
Or, if you'd rather use the
drawRect
approach rather than usingCAShapeLayer
:If you want the arc to occupy the bottom of the view, the path would look like:
Essentially, that's the same
theta
andradius
, but start in lower left corner, set thecenter
to besize.width / 2.0, radius
, and arc fromM_PI_2 * 3.0
±theta
: