Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGestureRecogniser on a masked UIView?

Is there a way to know if a 'tap' is inside or outside the masked area of a UIView? I'm using CoreGraphics to mask the UIView.

Tap Location Diagram

So far my code goes something like this..

- (void)viewDidLoad {

    UIGestureRecogniser *r = [[UIGestureRecogniser alloc] initWithTarget:self action:@selector(gestCall:)];
    [self addGestureRecogniser:r];

}

- (void)gestCall:(UIGestureRecogniser *)gestRec {
     if ("somthing") {
        // outside of mask
     } else {
        // inside of mask
     }
}

Thank you.

like image 546
sobox studio Avatar asked Dec 10 '25 14:12

sobox studio


2 Answers

I've finally found the solution I was looking for. So for the benefit of any one trying to find is a CGPoint is inside any CGPath.

It's simple.

UIBezierPath *p = [UIBezierPath bezierPathWithCGPath:anyCGPath];

BOOL isInPath = [p containsPoint:anyCGPoint];
like image 157
sobox studio Avatar answered Dec 13 '25 02:12

sobox studio


Basically you need to check the touch coordinate and decide whether is falls into the mask area or not. Override the hitTest:withEvent: and account for the image mask. You can use [[[self layer] presentationLayer] hitTest:aPoint] or [[[self layer] mask] hitTest:aPoint] in your overridden `-[UIView hitTest:withEvent:].

[EDIT]

Check if a user tapped near a CGPath might help to find answer to your question.

[EDIT]

Do following in your Gesture Handler to figure out to process tap or not.

  1. Specify the center of the circle (This would be UIView.Center as CGPoint)
  2. Specify the radius of pie chart
  3. When user tap on the view, get the location as point - CGPoint and calculate point.x*point.x+point.y*point.y (Circle formulae) and this value must be less than or equal to the square of the radius i.e radius*radius. If this condition satisfied then your tap point is inside the circle otherwise outside.

Hope that makes clear.

like image 25
AlienMonkeyCoder Avatar answered Dec 13 '25 04:12

AlienMonkeyCoder