Wednesday, June 5, 2013

UIBezierPath addLineToPoint example in Objective C (iOS).

UIBezierPath addLineToPoint

Appends a straight line to the receiver’s path.

- (void)addLineToPoint:(CGPoint)point

The destination point of the line segment, specified in the current coordinate system.

Discussion of [UIBezierPath addLineToPoint]
This method creates a straight line segment starting at the current point and ending at the point specified by the point parameter. After adding the line segment, this method updates the current point to the value in point.

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.

UIBezierPath addLineToPoint example.
    UIBezierPath *path = [UIBezierPath bezierPath];

    CGRect pathRect = CGRectInset(self.animationLayer.bounds, 0.0f, 0.0f);   
// define cgpoint
    CGPoint number1 = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMaxY(pathRect));
    [path addLineToPoint: number1];

Example of [UIBezierPath addLineToPoint].
//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;

UIBezierPath addLineToPoint example.
 - (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];


End of UIBezierPath addLineToPoint example article.