Showing posts with label UIBezierPath example. Show all posts
Showing posts with label UIBezierPath example. Show all posts

Wednesday, June 5, 2013

UIBezierPath UIRectCornerAllCorners example in Objective C (iOS).


UIBezierPath UIRectCornerAllCorners

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerAllCorners]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerAllCorners example.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame
                                                           byRoundingCorners:UIRectCornerAllCorners
                                                                 cornerRadii:CGSizeMake(5.0, 5.0)];
            // Create the shape layer and set its path
            CAShapeLayer *maskLayer = [CAShapeLayer layer];
            maskLayer.frame = frame;
            maskLayer.path = maskPath.CGPath;
            // Set the newly created shape layer as the mask for the image view's layer
            imageView.layer.mask = maskLayer;
imageView.layer.cornerRadius = 5.0;

Example of [UIBezierPath UIRectCornerAllCorners].
- (void)drawRect:(CGRect)rect
{
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [[UIColor xxxx] CGColor];
    self.layer.shadowOffset = CGSizeMake(0,2);
    self.layer.shadowRadius = ...
    self.layer.shadowOpacity = ...

    UIBezierPath *path = [UIBezierPath bezierPathWithPartiallyRoundedRect:rect
           byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
    [[UIColor blackColor] setFill or Stroke];

    [path stroke or fill];
}

UIBezierPath UIRectCornerAllCorners example.
- (void) layoutSubviews {
    [super layoutSubviews];

    const CGFloat PageCellBackgroundRadius = 6.0;

    for(int i = 0; i < [self numberOfSections]; i++) {
        NSInteger viewTag = i + 123456;
        CGRect frameRect = [self shadowFrameForSection: i];

        UIView* shadowBackgroundView = [self viewWithTag: viewTag];
        if (shadowBackgroundView) {
            if (!CGRectEqualToRect(frameRect, shadowBackgroundView.frame)) {
                shadowBackgroundView.frame = frameRect;
                CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
                                                             byRoundingCorners: UIRectCornerAllCorners
                                                                   cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
                shadowBackgroundView.layer.shadowPath = shadowPath;
            }

            [self sendSubviewToBack: shadowBackgroundView];
        } else {
            shadowBackgroundView = [[[UIView alloc] initWithFrame: frameRect] autorelease];
            shadowBackgroundView.tag = viewTag;
            shadowBackgroundView.opaque = YES;
            shadowBackgroundView.backgroundColor = [UIColor clearColor];

            shadowBackgroundView.layer.shadowOpacity = 0.3;
            shadowBackgroundView.layer.shadowRadius = 2;
            shadowBackgroundView.layer.shadowColor = [[UIColor blackColor] CGColor];
            shadowBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 1.0);
            CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
                                                         byRoundingCorners: UIRectCornerAllCorners
                                                               cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
            shadowBackgroundView.layer.shadowPath = shadowPath;
            shadowBackgroundView.layer.shouldRasterize = YES;

            [self addSubview: shadowBackgroundView];
        }
    }
}

End of UIBezierPath UIRectCornerAllCorners example article.

UIBezierPath UIRectCornerBottomRight example in Objective C (iOS).


UIBezierPath UIRectCornerBottomRight

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerBottomRight]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerBottomRight example.
// Create the path (with only the bottom corners rounded)
    maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bodyBackgroundImageView.bounds
                                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                           cornerRadii:CGSizeMake(10.0, 10.0)];

    // Create the shape layer and set its path
    maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.titleBackgroundImageView.bounds;
    maskLayer.path = maskPath.CGPath;

    cell.bodyBackgroundImageView.layer.mask = maskLayer;

Example of [UIBezierPath UIRectCornerBottomRight].
UIView *containerView = [[UIView alloc] initWithFrame:someFrame];

UIRectCorners corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
CGSize radii = CGSizeMake(kThisViewCornerRadius, kThisViewCornerRadius);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                           byRoundingCorners:corners
                                                 cornerRadii:radii];

// Mask the container view’s layer to round the corners.
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
[cornerMaskLayer setPath:path.CGPath];
containerView.layer.mask = cornerMaskLayer;

// Make a transparent, stroked layer which will dispay the stroke.
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = path.CGPath;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.strokeColor = [UIColor redColor].CGColor;
strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside,
                           // but the outside part will be clipped by the containerView’s mask.

// Transparent view that will contain the stroke layer
UIView *strokeView = [[UIView alloc] initWithFrame:containerView.bounds];
strokeView.userInteractionEnabled = NO; // in case your container view contains controls
[strokeView.layer addSublayer:strokeLayer];

// configure and add any subviews to the container view

// stroke view goes in last, above all the subviews
[containerView addSubview:strokeView];

UIBezierPath UIRectCornerBottomRight example.
- (void) drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0f, 10.0f)];

    [[UIColor blackColor] setFill];
    [path fill];

    CGRect innerRect = CGRectInset(rect, 4.0f, 2.0f);
    innerRect.origin.y -= 2.0f;

    UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:innerRect
        byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0f, 8.0f)];

    [[UIColor redColor] setFill];
    [innerPath fill];
}

End of UIBezierPath UIRectCornerBottomRight example article.

UIBezierPath UIRectCornerBottomLeft example in Objective C (iOS).


UIBezierPath UIRectCornerBottomLeft

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerBottomLeft]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerBottomLeft example.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: btnView.bounds
                                               byRoundingCorners:UIRectCornerBottomLeft
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = btnView.bounds;
maskLayer.path = maskPath.CGPath;
btnView.layer.mask = maskLayer;
[btnView setNeedsDisplay];

Example of [UIBezierPath UIRectCornerBottomLeft].
UIView *containerView = [[UIView alloc] initWithFrame:someFrame];

UIRectCorners corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
CGSize radii = CGSizeMake(kThisViewCornerRadius, kThisViewCornerRadius);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                           byRoundingCorners:corners
                                                 cornerRadii:radii];

// Mask the container view’s layer to round the corners.
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
[cornerMaskLayer setPath:path.CGPath];
containerView.layer.mask = cornerMaskLayer;

// Make a transparent, stroked layer which will dispay the stroke.
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = path.CGPath;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.strokeColor = [UIColor redColor].CGColor;
strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside,
                           // but the outside part will be clipped by the containerView’s mask.

// Transparent view that will contain the stroke layer
UIView *strokeView = [[UIView alloc] initWithFrame:containerView.bounds];
strokeView.userInteractionEnabled = NO; // in case your container view contains controls
[strokeView.layer addSublayer:strokeLayer];

// configure and add any subviews to the container view

// stroke view goes in last, above all the subviews
[containerView addSubview:strokeView];

UIBezierPath UIRectCornerBottomLeft example.
- (void) drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0f, 10.0f)];

    [[UIColor blackColor] setFill];
    [path fill];

    CGRect innerRect = CGRectInset(rect, 4.0f, 2.0f);
    innerRect.origin.y -= 2.0f;

    UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:innerRect
        byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0f, 8.0f)];

    [[UIColor redColor] setFill];
    [innerPath fill];
}

End of UIBezierPath UIRectCornerBottomLeft example article.

UIBezierPath UIRectCornerTopRight example in Objective C (iOS).


UIBezierPath UIRectCornerTopRight

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerTopRight]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerTopRight example.
UIBezierPath *bigMaskPath = [UIBezierPath bezierPathWithRoundedRect:bigView.bounds
                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                       cornerRadii:CGSizeMake(18, 18)];
UIBezierPath *smallMaskPath = [UIBezierPath bezierPathWithRoundedRect:smalLView.bounds
                                     byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                           cornerRadii:CGSizeMake(18, 18)];

UIBezierPath *finalPath = [UIBezierPath pathBySubtractingPath:smallMaskPath fromPath:bigMaskPath];

Example of [UIBezierPath UIRectCornerTopRight].
CAShapeLayer *topLayer = [CAShapeLayer layer];
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft)
    cornerRadii:CGSizeMake(9.f, 9.0f)];   
topLayer.path = [roundedPath CGPath];

UIBezierPath UIRectCornerTopRight example.
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
[path addClip];

[_image drawInRect:rect];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

End of UIBezierPath UIRectCornerTopRight example article.

UIBezierPath UIRectCornerTopLeft example in Objective C (iOS).


UIBezierPath UIRectCornerTopLeft

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerTopLeft]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerTopLeft example.
UIBezierPath *bigMaskPath = [UIBezierPath bezierPathWithRoundedRect:bigView.bounds
                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                       cornerRadii:CGSizeMake(18, 18)];
UIBezierPath *smallMaskPath = [UIBezierPath bezierPathWithRoundedRect:smalLView.bounds
                                     byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                           cornerRadii:CGSizeMake(18, 18)];

UIBezierPath *finalPath = [UIBezierPath pathBySubtractingPath:smallMaskPath fromPath:bigMaskPath];

Example of [UIBezierPath UIRectCornerTopLeft].
CAShapeLayer *topLayer = [CAShapeLayer layer];
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft)
    cornerRadii:CGSizeMake(9.f, 9.0f)];   
topLayer.path = [roundedPath CGPath];

UIBezierPath UIRectCornerTopLeft example.
if (indexPath.row == 0)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayPeople.count - 1)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];

End of UIBezierPath UIRectCornerTopLeft example article.

UIBezierPath strokeWithBlendMode alpha example in Objective C (iOS).


UIBezierPath strokeWithBlendMode alpha

Draws a line along the receiver’s path using the specified blend mode and transparency values.

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha

Parameters of [UIBezierPath strokeWithBlendMode alpha]
blendMode
The blend mode determines how the stroked path is composited with any existing rendered content.
alpha
The amount of transparency to apply to the stroked path. Values can range between 0.0 (transparent) and 1.0 (opaque). Values outside this range are clamped to 0.0 or 1.0.

Discussion of [UIBezierPath strokeWithBlendMode alpha]
The drawn line is centered on the path with its sides parallel to the path segment. This method applies the current stroke color and drawing properties (plus the specified blend mode and transparency value) to the rendered path.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

UIBezierPath strokeWithBlendMode alpha example.
int count = 0;
for(UIBezierpath *_paths in pathArray)
{
   UIColor *_color = [delegate1.colorArray objectAtIndex:q];
   [_color setStroke];
   [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
   count++;
}

Example of [UIBezierPath strokeWithBlendMode alpha].
 - (void)drawRect:(CGRect)rect
    {
        [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        for(UIBezierPath *_path in pathArray)
            [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }

UIBezierPath strokeWithBlendMode alpha example.
 - (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [strokeColor setStroke];
    if (!incrementalImage) // first time draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor colorWithPatternImage:[UIImage imageNamed:@"two.jpg"]] setFill];
        //[[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
        NSLog(@"========== ... drawBitmap .. CALLED=======");
    }
    [incrementalImage drawAtPoint:CGPointZero];

    for(NSDictionary *_pathDict in pathArray)
    {
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }

End of UIBezierPath strokeWithBlendMode alpha example article.

UIBezierPath strokeWithBlendMode example in Objective C (iOS).


UIBezierPath strokeWithBlendMode

Draws a line along the receiver’s path using the specified blend mode and transparency values.

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha

Parameters of [UIBezierPath strokeWithBlendMode]
blendMode
The blend mode determines how the stroked path is composited with any existing rendered content.
alpha
The amount of transparency to apply to the stroked path. Values can range between 0.0 (transparent) and 1.0 (opaque). Values outside this range are clamped to 0.0 or 1.0.

Discussion of [UIBezierPath strokeWithBlendMode]
The drawn line is centered on the path with its sides parallel to the path segment. This method applies the current stroke color and drawing properties (plus the specified blend mode and transparency value) to the rendered path.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

UIBezierPath strokeWithBlendMode example.
int count = 0;
for(UIBezierpath *_paths in pathArray)
{
   UIColor *_color = [delegate1.colorArray objectAtIndex:q];
   [_color setStroke];
   [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
   count++;
}

Example of [UIBezierPath strokeWithBlendMode].
 - (void)drawRect:(CGRect)rect
    {
        [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        for(UIBezierPath *_path in pathArray)
            [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }

UIBezierPath strokeWithBlendMode example.
 - (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [strokeColor setStroke];
    if (!incrementalImage) // first time draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor colorWithPatternImage:[UIImage imageNamed:@"two.jpg"]] setFill];
        //[[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
        NSLog(@"========== ... drawBitmap .. CALLED=======");
    }
    [incrementalImage drawAtPoint:CGPointZero];

    for(NSDictionary *_pathDict in pathArray)
    {
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }

End of UIBezierPath strokeWithBlendMode example article.

UIBezierPath removeAllPoints example in Objective C (iOS).


UIBezierPath removeAllPoints

Removes all points from the receiver, effectively deleting all subpaths.

- (void)removeAllPoints

UIBezierPath removeAllPoints example.

- (void)drawRect:(CGRect)rect
{
    if (_uipath == NULL)
       _uipath = [[UIBezierPath alloc] init];
    else
        [_uipath removeAllPoints];

    [self drawRect:rect  :self.graphPoints :self.drawingType ];
}

Example of [UIBezierPath removeAllPoints].

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [path removeAllPoints];
    [self setNeedsDisplay];
    ctr = 0;
}

UIBezierPath removeAllPoints example.
- (UIBezierPath*)smoothedPathWithGranularity:(NSInteger)granularity
{
    NSMutableArray *points = [(NSMutableArray*)[self pointsOrdered] mutableCopy];

    if (points.count < 4) return [self bezierPath];

    // Add control points to make the math make sense
    [points insertObject:[points objectAtIndex:0] atIndex:0];
    [points addObject:[points lastObject]];

    UIBezierPath *smoothedPath = [self bezierPath];
    [smoothedPath removeAllPoints];

    [smoothedPath moveToPoint:POINT(0)];

    for (NSUInteger index = 1; index < points.count - 2; index++)
    {
        CGPoint p0 = POINT(index - 1);
        CGPoint p1 = POINT(index);
        CGPoint p2 = POINT(index + 1);
        CGPoint p3 = POINT(index + 2);

        // now add n points starting at p1 + dx/dy up until p2 using Catmull-Rom splines
        for (int i = 1; i < granularity; i++)
        {
            float t = (float) i * (1.0f / (float) granularity);
            float tt = t * t;
            float ttt = tt * t;

            CGPoint pi; // intermediate point
            pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
            pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
            [smoothedPath addLineToPoint:pi];
        }

        // Now add p2
        [smoothedPath addLineToPoint:p2];
    }

    // finish by adding the last point
    [smoothedPath addLineToPoint:POINT(points.count - 1)];

    return smoothedPath;
}

End of UIBezierPath removeAllPoints example article.

UIBezierPath setLineDash count phase example in Objective C (iOS).


UIBezierPath setLineDash count phase

Sets the line-stroking pattern for the path.

- (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase

Parameters of [UIBezierPath setLineDash count phase]
pattern
A C-style array of floating point values that contains the lengths (measured in points) of the line segments and gaps in the pattern. The values in the array alternate, starting with the first line segment length, followed by the first gap length, followed by the second line segment length, and so on.
count
The number of values in pattern.
phase
The offset at which to start drawing the pattern, measured in points along the dashed-line pattern. For example, a phase value of 6 for the pattern 5-2-3-2 would cause drawing to begin in the middle of the first gap.

UIBezierPath setLineDash count phase example.
-(void)updateLine{

      // Important, otherwise we will be adding multiple sub layers
      if ([[[self layer] sublayers] objectAtIndex:0])
        {
            self.layer.sublayers = nil;
        }

        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setBounds:self.bounds];
        [shapeLayer setPosition:self.center];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
        [shapeLayer setLineWidth:3.0f];
        [shapeLayer setLineJoin:kCALineJoinRound];
        [shapeLayer setLineDashPattern:
        [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
        [NSNumber numberWithInt:5],nil]];

        // Setup the path
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, beginPoint.center.x, beginPoint.center.y);
        CGPathAddLineToPoint(path, NULL, endPoint.center.x, endPoint.center.y);

        [shapeLayer setPath:path];
        CGPathRelease(path);

        [[self layer] addSublayer:shapeLayer];
}

Example of [UIBezierPath setLineDash count phase].
-(void)setSelected:(BOOL) yes_no {
    selected = yes_no;
   if (yes_no == YES) {
        CGFloat dashArray[2];
        dashArray[0] = 5;
        dashArray[1] = 2;
        [self setLineDash:dashArray count:2 phase:0];
       self.pathColor = [self.unselectedColor highlightWithLevel:.5];
   } else {
       [self setLineDash:nil count:2 phase:0];
        self.pathColor = self.unselectedColor;
   }
}

UIBezierPath setLineDash count phase example.
UIBezierPath *path = [UIBezierPath new];
CGFloat dashArray[3];
dashArray[0] = 8;
dashArray[1] = 3;
dashArray[2] = 8;
[path setLineDash:dashArray count:dashCount phase: 0.0];

End of UIBezierPath setLineDash count phase example article.

UIBezierPath setLineDash example in Objective C (iOS).


UIBezierPath setLineDash

Sets the line-stroking pattern for the path.

- (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase

Parameters of [UIBezierPath setLineDash]
pattern
A C-style array of floating point values that contains the lengths (measured in points) of the line segments and gaps in the pattern. The values in the array alternate, starting with the first line segment length, followed by the first gap length, followed by the second line segment length, and so on.
count
The number of values in pattern.
phase
The offset at which to start drawing the pattern, measured in points along the dashed-line pattern. For example, a phase value of 6 for the pattern 5-2-3-2 would cause drawing to begin in the middle of the first gap.

UIBezierPath setLineDash example.
-(void)updateLine{

      // Important, otherwise we will be adding multiple sub layers
      if ([[[self layer] sublayers] objectAtIndex:0])
        {
            self.layer.sublayers = nil;
        }

        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setBounds:self.bounds];
        [shapeLayer setPosition:self.center];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
        [shapeLayer setLineWidth:3.0f];
        [shapeLayer setLineJoin:kCALineJoinRound];
        [shapeLayer setLineDashPattern:
        [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
        [NSNumber numberWithInt:5],nil]];

        // Setup the path
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, beginPoint.center.x, beginPoint.center.y);
        CGPathAddLineToPoint(path, NULL, endPoint.center.x, endPoint.center.y);

        [shapeLayer setPath:path];
        CGPathRelease(path);

        [[self layer] addSublayer:shapeLayer];
}

Example of [UIBezierPath setLineDash].
-(void)setSelected:(BOOL) yes_no {
    selected = yes_no;
   if (yes_no == YES) {
        CGFloat dashArray[2];
        dashArray[0] = 5;
        dashArray[1] = 2;
        [self setLineDash:dashArray count:2 phase:0];
       self.pathColor = [self.unselectedColor highlightWithLevel:.5];
   } else {
       [self setLineDash:nil count:2 phase:0];
        self.pathColor = self.unselectedColor;
   }
}

UIBezierPath setLineDash example.
UIBezierPath *path = [UIBezierPath new];
CGFloat dashArray[3];
dashArray[0] = 8;
dashArray[1] = 3;
dashArray[2] = 8;
[path setLineDash:dashArray count:dashCount phase: 0.0];

End of UIBezierPath setLineDash example article.

UIBezierPath moveToPoint example in Objective C (iOS).


UIBezierPath moveToPoint

Moves the receiver’s current point to the specified location.

- (void)moveToPoint:(CGPoint)point

Parameters
point
A point in the current coordinate system.

Discussion of [UIBezierPath moveToPoint]
This method implicitly ends the current subpath (if any) and sets the current point to the value in the point parameter. When ending the previous subpath, this method does not actually close the subpath. Therefore, the first and last points of the previous subpath are not connected to each other.[UIBezierPath moveToPoint]

For many path operations, you must call this method before issuing any commands that cause a line or curve segment to be drawn.

UIBezierPath moveToPoint example.
//add this method to your class
- (UIBezierPath *)myPath {
    static UIBezierPath *path = nil;
    if(!path) {
        path = [[UIBezierPath bezierPath] retain];
        [path moveToPoint:CGPoingMake(200.053,79.688)];
        [path addLineToPoint:CGPointMake(100.053,179.688)];
        [path addLineToPoint:CGPointMake(304.412,280.125)];
        [path addLineToPoint:CGPointMake(308.055,298.513)];
        [path addLineToPoint:CGPointMake(200.053,79.688)];
        [path closePath];
        path.lineWidth = 5;
    }
    return path;
}

Example of [UIBezierPath moveToPoint].
    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }

UIBezierPath moveToPoint example.
-(CGPathRef)makeToPath CF_RETURNS_RETAINED;
{
    UIBezierPath* triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:CGPointZero];
    [triangle addLineToPoint:CGPointMake(self.view.frame.size.width,0)];
    [triangle addLineToPoint:CGPointMake(0, self.view.frame.size.height)];
    [triangle closePath];
    CGPathRef theCGPath = [triangle CGPath];
    return CGPathCreateCopy(theCGPath);
}

End of UIBezierPath moveToPoint example article.

UIBezierPath fillWithBlendMode alpha example in Objective C (iOS).


UIBezierPath fillWithBlendMode alpha

Paints the region enclosed by the receiver’s path using the specified blend mode and transparency values.

- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha

Parameters of [UIBezierPath fillWithBlendMode alpha]
blendMode
The blend mode determines how the filled path is composited with any existing rendered content.
alpha
The amount of transparency to apply to the filled path. Values can range between 0.0 (transparent) and 1.0 (opaque). Values outside this range are clamped to 0.0 or 1.0.

Discussion of [UIBezierPath fillWithBlendMode alpha]
This method fills the path using the current fill color and drawing properties (plus the specified blend mode and transparency value). If the path contains any open subpaths, this method implicitly closes them before painting the fill region.

The painted region includes the pixels right up to, but not including, the path line itself. For paths with large line widths, this can result in overlap between the fill region and the stroked path (which is itself centered on the path line).[UIBezierPath fillWithBlendMode alpha]

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

UIBezierPath fillWithBlendMode alpha example.
CGRect container = CGRectMake(conX, conY, 220, 50);
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0];
    [[UIColor blueColor] setFill];
    [path fillWithBlendMode:kCGBlendModeNormal alpha:0.7];

Example of [UIBezierPath fillWithBlendMode alpha].
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 maskLayer.bounds.size.width,
                                                 maskLayer.bounds.size.height,
                                                 8,
                                                 maskLayer.bounds.size.width,
                                                 NULL,
                                                 kCGImageAlphaOnly);
    UIGraphicsPushContext(context);
    CGContextSetAlpha(context, 1.0);
    CGContextFillRect(context, maskLayer.bounds);
    [boxPath fillWithBlendMode:kCGBlendModeCopy alpha:0.0];
    CGImageRef maskImage = CGBitmapContextCreateImage(context);
    maskLayer.contents = (__bridge id)maskImage;
    UIGraphicsPopContext();
    CGImageRelease(maskImage);
    CGContextRelease(context);

End of UIBezierPath fillWithBlendMode alpha example article.

UIBezierPath fillWithBlendMode example in Objective C (iOS).


UIBezierPath fillWithBlendMode

Paints the region enclosed by the receiver’s path using the specified blend mode and transparency values.

- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha

Parameters of [UIBezierPath fillWithBlendMode]
blendMode
The blend mode determines how the filled path is composited with any existing rendered content.
alpha
The amount of transparency to apply to the filled path. Values can range between 0.0 (transparent) and 1.0 (opaque). Values outside this range are clamped to 0.0 or 1.0.

Discussion of [UIBezierPath fillWithBlendMode]
This method fills the path using the current fill color and drawing properties (plus the specified blend mode and transparency value). If the path contains any open subpaths, this method implicitly closes them before painting the fill region.

The painted region includes the pixels right up to, but not including, the path line itself. For paths with large line widths, this can result in overlap between the fill region and the stroked path (which is itself centered on the path line).[UIBezierPath fillWithBlendMode]

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

UIBezierPath fillWithBlendMode example.
CGRect container = CGRectMake(conX, conY, 220, 50);
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0];
    [[UIColor blueColor] setFill];
    [path fillWithBlendMode:kCGBlendModeNormal alpha:0.7];

Example of [UIBezierPath fillWithBlendMode].
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 maskLayer.bounds.size.width,
                                                 maskLayer.bounds.size.height,
                                                 8,
                                                 maskLayer.bounds.size.width,
                                                 NULL,
                                                 kCGImageAlphaOnly);
    UIGraphicsPushContext(context);
    CGContextSetAlpha(context, 1.0);
    CGContextFillRect(context, maskLayer.bounds);
    [boxPath fillWithBlendMode:kCGBlendModeCopy alpha:0.0];
    CGImageRef maskImage = CGBitmapContextCreateImage(context);
    maskLayer.contents = (__bridge id)maskImage;
    UIGraphicsPopContext();
    CGImageRelease(maskImage);
    CGContextRelease(context);

End of UIBezierPath fillWithBlendMode example article.

UIBezierPath containsPoint example in Objective C (iOS).


UIBezierPath containsPoint

Returns a Boolean value indicating whether the area enclosed by the receiver contains the specified point.

- (BOOL)containsPoint:(CGPoint)point

Parameters
point
The point to test against the path, specified in the path object's coordinate system.

Return Value of [UIBezierPath containsPoint]
YES if the point is considered to be within the path’s enclosed area or NO if it is not.

Discussion of [UIBezierPath containsPoint]
The receiver contains the specified point if that point is in a portion of a closed subpath that would normally be painted during a fill operation. This method uses the value of the usesEvenOddFillRule property to determine which parts of the subpath would be filled.

A point is not considered to be enclosed by the path if it is inside an open subpath, regardless of whether that area would be painted during a fill operation. Therefore, to determine mouse hits on open paths, you must create a copy of the path object and explicitly close any subpaths (using the closePath method) before calling this method.

UIBezierPath containsPoint example.
-  (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

     touchStart  =  [[touches anyObject] locationInView:self];

    isResizingUL = [upperLeft containsPoint:touchStart];
    isResizingUR= [upperRight containsPoint:touchStart];
}

Example of [UIBezierPath containsPoint].
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    if ([self.bezierPath containsPoint:touchPoint])
    {
        // do stuff
    }
}

UIBezierPath containsPoint example.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(![[self myPath] containsPoint:[[touches anyObject] locationInView:self]]) return;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
    //After some time
    [alert show];
    [alert release];
}

End of UIBezierPath containsPoint example article.

UIBezierPath closePath example in Objective C (iOS).


UIBezierPath closePath

Closes the most recently added subpath.

- (void)closePath

Discussion of [UIBezierPath closePath]
This method closes the current subpath by creating a line segment between the first and last points in the subpath. This method subsequently updates the current point to the end of the newly created line segment, which is also the first point in the now closed subpath.

UIBezierPath closePath example.
    UIBezierPath *aPath = [[UIBezierPath alloc] init];
        [aPath moveToPoint:CGPointMake(227,34.25)];
    [aPath addLineToPoint:CGPointMake(298.25,34.75)];
    [aPath addLineToPoint:CGPointMake(298.5,82.5)];
    [aPath addLineToPoint:CGPointMake(251,83)];
    [aPath addLineToPoint:CGPointMake(251,67.5)];
    [aPath addLineToPoint:CGPointMake(227.25,66.75)];  
        [aPath closePath];
    aPath.lineWidth = 2;
    [aPath fill];
    [aPath stroke];

Example of [UIBezierPath closePath].
    UIBezierPath*    aPath2 = [[UIBezierPath alloc] init];
    [aPath2 moveToPoint:CGPointMake(251.25,90.5)];
    [aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
    [aPath2 addLineToPoint:CGPointMake(298.5,83)];
    [aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
    [aPath2 closePath];
    aPath2.lineWidth = 2;
    [aPath2 fill];
    [aPath2 stroke];

UIBezierPath closePath example.
- (UIBezierPath *) createPath {
    static UIBezierPath *path = nil;
    if(!path) {
        path = [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(35, 45, 250, 250)] retain];
        path.lineWidth = 50.0;
        [path closePath];
    }   
    return path;
}

End of UIBezierPath closePath example article.

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.

UIBezierPath applyTransform example in Objective C (iOS).


UIBezierPath applyTransform

Transforms all points in the path using the specified affine transform matrix.

- (void)applyTransform:(CGAffineTransform)transform

Parameters
transform
The transform matrix to apply to the path.

Discussion of [UIBezierPath applyTransform]
This method applies the specified transform to the path’s points immediately. The modifications made to the path object are permanent. If you do not want to permanently modify a path object, you should consider applying the transform to a copy.

UIBezierPath applyTransform example.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, self.bounds.size.width/2.0f, self.bounds.size.height/2.0f);

    UIBezierPath *path = [self createPath];
    [path stroke];

    path = [self createPath];
    CGAffineTransform rot = CGAffineTransformMakeRotation(2 * M_PI/16);
    [path applyTransform:rot];
    [path stroke];

    path = [self createPath];
    rot = CGAffineTransformMakeRotation( 2 * M_PI/8);
    [path applyTransform:rot];
    [path stroke];
}

- (UIBezierPath *) createPath {
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint start = CGPointMake(0, 0);
    CGPoint end = CGPointMake(self.bounds.size.width/4.0f, 0);
    [path moveToPoint:start];
    [path addLineToPoint:end];
    return path;
}

Example of [UIBezierPath applyTransform].
CGAffineTransform translate = CGAffineTransformMakeTranslation(-1 * (box.origin.x + (box.size.width / 2)), -1 * (box.origin.y + (box.size.height / 2)));
[path applyTransform:translate];

CGAffineTransform rotate = CGAffineTransformMakeRotation(DegreesToRadians(90));
[path applyTransform:rotate];

translate = CGAffineTransformMakeTranslation((box.origin.x + (box.size.width / 2)), (box.origin.y + (box.size.height / 2)));
[path applyTransform:translate];

UIBezierPath applyTransform example.
- (void) scaleAllPaths: (CGFloat) scaleFactor
{
  CGAffineTransform transform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
  for (UIBezierPath *bezierPath in availablePaths){
    [bezierPath applyTransform:transform];
  }
}

- (void)drawRect:(CGRect)rect
{
  ...
  for (int i = 0; i < currentPathIndex; i++) {
    UIBezierPath *bezierPath = [availablePaths objectAtIndex:i];
    CGPathRef path = bezierPath.CGPath;
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathEOFill);
  }
  ...
}

End of UIBezierPath applyTransform example article.

UIBezierPath appendPath example in Objective C (iOS).


UIBezierPath appendPath

Appends the contents of the specified path object to the receiver’s path.

- (void)appendPath:(UIBezierPath *)bezierPath

Parameters
bezierPath
The path to add to the receiver.

Discussion of [UIBezierPath appendPath]
This method adds the commands used to create the path in bezierPath to the end of the receiver’s path. This method does not explicitly try to connect the subpaths in the two objects, although the operations in bezierPath might still cause that effect.

UIBezierPath appendPath example.
CGRect outerRect = {0, 0, 200, 200};
CGRect innerRect  = CGRectInset(outerRect,  30, 30);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:outerRect cornerRadius:10];

[path appendPath:[UIBezierPath bezierPathWithRoundedRect:innerRect cornerRadius:5]];
path.usesEvenOddFillRule = YES;

[[UIColor orangeColor] set];
[path fill];

Example of [UIBezierPath appendPath].
CGRect rect1, rect2;
CGFloat radius;

  // fill in the values you want for the rects and the radius
UIBezierPath *result =
    [UIBezierPath bezierPathWithRoundedRect: rect1 cornerRadius:radius];
[result appendPath:
    [UIBezierPath bezierPathWithRoundedRect: rect2 cornerRadius:radius];

 // result is now a path comprising both of the roundrects.  You can fill it with a gradient like any other path.

UIBezierPath appendPath example.
- (void)makeCGPath
{
    UIBezierPath *path = [[UIBezierPath alloc] init];

    if (lines && lines.count>0)
    {
        for (int i = 0; i < lines.count; i++)
        {
            UIBezierPath *linePath = [[UIBezierPath alloc] init];

            NSArray *tempArray = [lines objectAtIndex:i];
            CGPoint p = [[tempArray objectAtIndex:0]CGPointValue];
            [linePath addLineToPoint:p];
            for (int j = 1; j < tempArray.count; j++)
            {
                p = [[tempArray objectAtIndex:j]CGPointValue];
                [linePath addLineToPoint:p];
            }
            [path appendPath:linePath];
        }
    }

    if (points && points.count > 0)
    {
        UIBezierPath *pointPath = [[UIBezierPath alloc] init];
        CGPoint p = [[points objectAtIndex:0]CGPointValue];
        [pointPath moveToPoint:p];
        for (int i = 1; i < points.count;i++ )
        {
            p = [[points objectAtIndex:i]CGPointValue];
            [pointPath moveToPoint:p];
        }
        [path appendPath:pointPath];
    }

    drawPath = path.CGPath;
    [self setNeedsDisplay];

    [lines removeAllObjects];
    [points removeAllObjects];
}

End of UIBezierPath appendPath example article.

UIBezierPath addQuadCurveToPoint controlPoint example in Objective C (iOS).


UIBezierPath addQuadCurveToPoint controlPoint

Appends a quadratic Bézier curve to the receiver’s path.

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint

Parameters of [UIBezierPath addQuadCurveToPoint controlPoint]
endPoint
The end point of the curve.
controlPoint
The control point of the curve.

Discussion of [UIBezierPath addQuadCurveToPoint controlPoint]
This method appends a quadratic Bézier curve from the current point to the end point specified by the endPoint parameter. The relationships between the current point, control point, and end point are what defines the actual curve. Figure 3 shows some examples of quadratic curves and the approximate curve shape based on some sample points. The exact curvature of the segment involves a complex mathematical relationship between the points and is well documented online.

Figure 3 Quadratic curve examples
UIBezierPath addQuadCurveToPoint

You must set the path’s current point (using the moveToPoint: method or through the previous creation of a line or curve segment) before you call this method. If the path is empty, this method does nothing. After adding the curve segment, this method updates the current point to the value in point.
UIBezierPath addQuadCurveToPoint controlPoint example.
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 10)];
[path addQuadCurveToPoint:CGPointMake(200, 10) controlPoint:CGPointMake(100, 5)];
[path addLineToPoint:CGPointMake(200, 0)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];

CGContextAddPath(context, path.CGPath);
[[UIColor redColor] set];
CGContextFillPath(context);

Example of [UIBezierPath addQuadCurveToPoint controlPoint].
void MyCGPathApplierFunc (void *info, const CGPathElement *element) {

UIBezierPath *drawingPath = (UIBezierPath *)info;
CGPoint *points = element->points;
CGPathElementType type = element->type;

switch(type) {
    case kCGPathElementMoveToPoint: // contains 1 point
        [drawingPath moveToPoint:[[NSValue valueWithCGPoint:points[0]] CGPointValue]];
        break;

    case kCGPathElementAddLineToPoint: // contains 1 point
        [drawingPath addLineToPoint:[[NSValue valueWithCGPoint:points[0]] CGPointValue]];
        break;
    case kCGPathElementAddQuadCurveToPoint: // contains 1 point
        [drawingPath addQuadCurveToPoint:[[NSValue valueWithCGPoint:points[0]] CGPointValue]];
        break;

   case kCGPathElementAddCurveToPoint: // contains 1 point
        [drawingPath addCurveToPoint:[[NSValue valueWithCGPoint:points[0]] CGPointValue]];
        break;

    case kCGPathElementCloseSubpath: // contains no point
        break;
}
}

UIBezierPath addQuadCurveToPoint controlPoint example.
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path setLineWidth:3.0];
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineJoinStyle:kCGLineJoinRound];

    // actualPoints are my points array stored as NSValue

    NSValue *value = [actualPoints objectAtIndex:0];
    CGPoint p1 = [value CGPointValue];
    [path moveToPoint:p1];

    for (int k=1; k<[actualPoints count];k++) {

        NSValue *value = [actualPoints objectAtIndex:k];
        CGPoint p2 = [value CGPointValue];

        CGPoint centerPoint = CGPointMake((p1.x+p2.x)/2, (p1.y+p2.y)/2);

        // See if your curve is decreasing or increasing
        // You can optimize it further by finding point on normal of line passing through midpoint

        if (p1.y<p2.y) {
             centerPoint = CGPointMake(centerPoint.x, centerPoint.y+(abs(p2.y-centerPoint.y)));
        }else if(p1.y>p2.y){
             centerPoint = CGPointMake(centerPoint.x, centerPoint.y-(abs(p2.y-centerPoint.y)));
        }

        [path addQuadCurveToPoint:p2 controlPoint:centerPoint];
        p1 = p2;
    }

    [path stroke];

End of UIBezierPath addQuadCurveToPoint controlPoint example article.