Wednesday, June 5, 2013

UIBezierPath bezierPathWithArcCenter example in Objective C (iOS).

UIBezierPath bezierPathWithArcCenter

Creates and returns a new UIBezierPath object initialized with an arc of a circle.

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

Parameters of [UIBezierPath bezierPathWithArcCenter]
Specifies the center point of the circle (in the current coordinate system) used to define the arc.
Specifies the radius of the circle used to define the arc.
Specifies the starting angle of the arc (measured in radians).
Specifies the end angle of the arc (measured in radians).
The direction in which to draw the arc.

Return Value of [UIBezierPath bezierPathWithArcCenter]
A new path object with the specified arc.

Discussion of [UIBezierPath bezierPathWithArcCenter]
This method creates an open subpath. The created arc lies on the perimeter of the specified circle. When drawn in the default coordinate system, the start and end angles are based on the unit circle shown in Figure 1. For example, specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter to YES draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to NO draws the top half of the circle.

Figure 1 Angles in the default coordinate system

After calling this method, the current point is set to the point on the arc at the end angle of the circle.
UIBezierPath bezierPathWithArcCenter example.
-(void) createPaths {
    largeCircle = [UIBezierPath radius:LARGE_RADIUS startAngle:0 endAngle:RADIANS(360) clockwise:YES];
    whiteCircle = [UIBezierPath radius:SMALL_RADIUS startAngle:0 endAngle:RADIANS(360) clockwise:YES];   

    orangeWedge = [UIBezierPath bezierPath];

    double startAngle = RADIANS(45);
    double endAngle = RADIANS(135);

    [orangeWedge addLineToPoint:CGPointMake(sin(startAngle) +, cos(startAngle) +];
    [orangeWedge radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];

    startAngle = RADIANS(60);
    endAngle = RADIANS(120);

    yellowWedge = [UIBezierPath bezierPath];

    [yellowWedge addLineToPoint:CGPointMake(sin(startAngle) +, cos(startAngle) +];
    [yellowWedge radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];

    startAngle = RADIANS(75);
    endAngle = RADIANS(105);

    greenWedge = [UIBezierPath bezierPath];

    [greenWedge addLineToPoint:CGPointMake(sin(startAngle) +, cos(startAngle) +];
    [greenWedge radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];

Example of [UIBezierPath bezierPathWithArcCenter].
 to reverse your path, you'd just:

UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:center radius:200 startAngle:0 endAngle:180 clockwise:YES];
self.myPath = [aPath bezierPathByReversingPath];

UIBezierPath bezierPathWithArcCenter example.
upperLeft = [UIBezierPath bezierPathWithArcCenter:CGPointMake(xCorner + margin, yCorner + margin)
        [[UIColor blackColor]setFill];
        [upperLeft fill];
        [upperLeft closePath];

upperRight = [UIBezierPath bezierPathWithArcCenter:CGPointMake(xCorner+ widths - margin, yCorner + margin)
    [[UIColor blackColor]setFill];
    [upperRight fill];
    [upperRight closePath];

End of UIBezierPath bezierPathWithArcCenter example article.