Wednesday, June 5, 2013

UIBezierPath bezierPathByReversingPath example in Objective C (iOS).


UIBezierPath bezierPathByReversingPath

Creates and returns a new bezier path object with the reversed contents of the current path.

- (UIBezierPath *)bezierPathByReversingPath

Return Value
A new path object with the same path shape but for which the path has been created in the reverse direction.

Discussion of [UIBezierPath bezierPathByReversingPath]
Reversing a path does not necessarily change the appearance of the path when rendered. Instead, it changes the direction in which path segments are drawn. For example, reversing the path of a rectangle (whose line segments are normally drawn starting at the origin and proceeding in a counterclockwise direction) causes its line segments to be drawn in a clockwise direction instead. Drawing a reversed path could affect the appearance of a filled pattern, depending on the pattern and the fill rule in use.

This method reverses each whole or partial subpath in the path object individually.

UIBezierPath bezierPathByReversingPath example.
Use bezierPathByReversingPath. From the docs (iOS 6.0+ only):

Creates and returns a new bezier path object with the reversed contents of the current path.

so to reverse your path, you'd just:

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

Example of [UIBezierPath bezierPathByReversingPath].
-(void)cropIt
{
    if (pathClosed) {
        NSLog(@"path closed crop now");
        [pathMain appendPath:[pathCurrent bezierPathByReversingPath]];
        pathMain.usesEvenOddFillRule=YES;
        [self setClippingPath:[pathMain bezierPathByReversingPath] imageViewObj:self.imageToBeCropped];
        sendImageToBase(self.imageToBeCropped);
    }

}

UIBezierPath bezierPathByReversingPath example.
    UIView *greenCircle=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    greenCircle.center=touchpoint;
    greenCircle.backgroundColor=[UIColor greenColor];
        [greenCircle setTag:ctr];
    [greenCircle.layer setCornerRadius:10];
    [self addSubview:greenCircle];
        [pathMain appendPath:[pathCurrent bezierPathByReversingPath]];
        pathMain.usesEvenOddFillRule=YES;
        [pathCurrent moveToPoint:[[ptsNew objectAtIndex:ptsNew.count-2] CGPointValue]];
        [pathCurrent addCurveToPoint:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue] controlPoint1:firstControllPoint.center controlPoint2:[[ptsNew objectAtIndex:ptsNew.count-1] CGPointValue]]; // this is how a Bezier curve is appended to a path
        [self setNeedsDisplay];

End of UIBezierPath bezierPathByReversingPath example article.