Wednesday, June 5, 2013

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.