Wednesday, June 5, 2013

UIBezierPath addArcWithCenter example in Objective C (iOS).

UIBezierPath addArcWithCenter

Appends an arc to the receiver’s path.

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

Parameters of [UIBezierPath addArcWithCenter]
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.

Discussion of [UIBezierPath addArcWithCenter]
This method adds the specified arc beginning at the current point. 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. [UIBezierPath addArcWithCenter] 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.

After calling this method, the current point is set to the point on the arc at the end angle of the circle.

UIBezierPath addArcWithCenter 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 addArcWithCenter].
CGPoint theCenter = CGPointMake(100., 100.);
CGFloat innerRadius = 50.;
CGFloat outerRadius = 60.;
CGFloat startAngle = M_PI;
CGFloat endAngle = 3*M_PI/2;

UIBezierPath *aSegment = [UIBezierPath bezierPath];
[aSegment addArcWithCenter:theCenter radius:innerRadius startAngle:startAngle endAngle:endAngle clockwise:YES];
[aSegment addArcWithCenter:theCenter radius:outerRadius startAngle:endAngle endAngle:startAngle clockwise:NO];
[aSegment closePath];
[[UIColor redColor] setFill];
[aSegment fill];

UIBezierPath addArcWithCenter example.
+ (UIBezierPath *) bezierPathForBackButtonInRect:(CGRect)rect withRoundingRadius:(CGFloat)radius{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint mPoint = CGPointMake(CGRectGetMaxX(rect) - radius, rect.origin.y);
    CGPoint ctrlPoint = mPoint;
    [path moveToPoint:mPoint];

    ctrlPoint.y += radius;
    mPoint.x += radius;
    mPoint.y += radius;
    if (radius > 0) [path addArcWithCenter:ctrlPoint radius:radius startAngle:M_PI + M_PI_2 endAngle:0 clockwise:YES];

    mPoint.y = CGRectGetMaxY(rect) - radius;
    [path addLineToPoint:mPoint];

    ctrlPoint = mPoint;
    mPoint.y += radius;
    mPoint.x -= radius;
    ctrlPoint.x -= radius;
    if (radius > 0) [path addArcWithCenter:ctrlPoint radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];

    mPoint.x = rect.origin.x + (10.0f);
    [path addLineToPoint:mPoint];

    [path addLineToPoint:CGPointMake(rect.origin.x, CGRectGetMidY(rect))];

    mPoint.y = rect.origin.y;
    [path addLineToPoint:mPoint];

    [path closePath];
    return path;

End of UIBezierPath addArcWithCenter example article.