Wednesday, June 5, 2013

UIBezierPath moveToPoint example in Objective C (iOS).

UIBezierPath moveToPoint

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

- (void)moveToPoint:(CGPoint)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.
    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.