简体   繁体   中英

Transparent zone of a bezier path doesn't allow user interaction

I've got a BezierPath which has the form of an arc more or less, the thing is that I want the user to be able to tap inside the arc (where it could be a door for being more clear) but not on the arc. But I can't do this, it's like the space inside the arc is also part of the BezierPath but the only thing is that it is transparent.

The code which creates the BezierPath is the following:

- (UIImageView*)drawBezierPath {
    //// Bezier Drawing
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(55.5, 643.5)];
    [bezierPath addLineToPoint: CGPointMake(55.5, 417)];
    [bezierPath addLineToPoint: CGPointMake(63, 399)];
    [bezierPath addLineToPoint: CGPointMake(69.5, 381.5)];
    [bezierPath addLineToPoint: CGPointMake(79, 363.5)];
    [bezierPath addLineToPoint: CGPointMake(91, 346)];
    [bezierPath addLineToPoint: CGPointMake(102, 331)];
    [bezierPath addLineToPoint: CGPointMake(114.5, 314.5)];
    [bezierPath addLineToPoint: CGPointMake(127.5, 301.5)];
    [bezierPath addLineToPoint: CGPointMake(142.5, 286)];
    [bezierPath addLineToPoint: CGPointMake(156.5, 273.5)];
    [bezierPath addLineToPoint: CGPointMake(171, 260)];
    [bezierPath addLineToPoint: CGPointMake(189.5, 247.5)];
    [bezierPath addLineToPoint: CGPointMake(207.5, 234.5)];
    [bezierPath addLineToPoint: CGPointMake(229.5, 222.5)];
    [bezierPath addLineToPoint: CGPointMake(250, 211)];
    [bezierPath addLineToPoint: CGPointMake(272.5, 202)];
    [bezierPath addLineToPoint: CGPointMake(295.5, 194)];
    [bezierPath addLineToPoint: CGPointMake(321, 186.5)];
    [bezierPath addLineToPoint: CGPointMake(349, 180.5)];
    [bezierPath addLineToPoint: CGPointMake(375, 179)];
    [bezierPath addLineToPoint: CGPointMake(398, 177.5)];
    [bezierPath addLineToPoint: CGPointMake(424.5, 177.5)];
    [bezierPath addLineToPoint: CGPointMake(448.5, 180.5)];
    [bezierPath addLineToPoint: CGPointMake(473, 184.5)];
    [bezierPath addLineToPoint: CGPointMake(498.5, 192)];
    [bezierPath addLineToPoint: CGPointMake(521.5, 200)];
    [bezierPath addLineToPoint: CGPointMake(544.5, 209.5)];
    [bezierPath addLineToPoint: CGPointMake(565.5, 220)];
    [bezierPath addLineToPoint: CGPointMake(584, 231)];
    [bezierPath addLineToPoint: CGPointMake(603, 244)];
    [bezierPath addLineToPoint: CGPointMake(623.5, 259)];
    [bezierPath addLineToPoint: CGPointMake(640.5, 274)];
    [bezierPath addLineToPoint: CGPointMake(657.5, 290.5)];
    [bezierPath addLineToPoint: CGPointMake(673, 308)];
    [bezierPath addLineToPoint: CGPointMake(688.5, 327)];
    [bezierPath addLineToPoint: CGPointMake(702.5, 346.5)];
    [bezierPath addLineToPoint: CGPointMake(715, 368)];
    [bezierPath addLineToPoint: CGPointMake(727, 392.5)];
    [bezierPath addLineToPoint: CGPointMake(736.5, 414.5)];
    [bezierPath addLineToPoint: CGPointMake(736.5, 644)];
    [bezierPath addLineToPoint: CGPointMake(789.5, 644.5)];
    [bezierPath addLineToPoint: CGPointMake(789.5, 1)];
    [bezierPath addLineToPoint: CGPointMake(2.5, 1)];
    [bezierPath addLineToPoint: CGPointMake(2, 645)];
    [bezierPath addLineToPoint: CGPointMake(55.5, 643.5)];
    [bezierPath closePath];
    [[UIColor blackColor] setFill];
    [bezierPath fill];
    [[UIColor blackColor] setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];

    UIGraphicsBeginImageContext(CGSizeMake(800, 800));

    //this gets the graphic context
    CGContextRef context = UIGraphicsGetCurrentContext();

    //you can stroke and/or fill
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    [bezierPath fill];
    [bezierPath stroke];

    //now get the image from the context
    UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage];
    bezierImageView.alpha = 0;
    bezierImageView.frame = CGRectMake(115, 92, bezierImageView.frame.size.width, bezierImageView.frame.size.height);
    return bezierImageView;
}

In this method I create the bezierPath and the save it in a UIImageView for adding it to another view.

I thought that bezierPath serve for drawing strange forms and occuping only the space of that form, but now I don't know what to think!

Any ideas about that? thank you!

Step 1 : Add QuartzCore.Framwork in your Project

Step 2 : #import <QuartzCore/QuartzCore.h> in your .h file

Step 3 : Define a variable UIBezierPath *path; in your .h file

Step 4 : Draw a Trianble Shape in Your viewDidLoad method

path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(50, 200)];
[path addLineToPoint:CGPointMake(150, 200)];
[path closePath];
[path setLineWidth:2.0f];

// Draw a shape Layer with Triangle Path
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillColor   = [UIColor redColor].CGColor;
shapeLayer.strokeColor = [UIColor yellowColor].CGColor;
shapeLayer.lineWidth   = 2;
shapeLayer.path = path.CGPath;

// Add it to your view
[self.view.layer addSublayer:shapeLayer];

Step 5 : Implement following Touch Method to detect touch on View & Shape

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint currentPoint = [touch locationInView:self.view];
    if([path containsPoint:currentPoint])
    {
        NSLog(@"its Inside Triangle Path");
    }
    else
    {
        NSLog(@"OutSide Shape");
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if([path containsPoint:currentPoint])
    {
        NSLog(@"its Inside Triangle Path");
    }
    else
    {
        NSLog(@"OutSide Shape");
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if([path containsPoint:currentPoint])
    {
        NSLog(@"its Inside Triangle Path");
    }
    else
    {
        NSLog(@"OutSide Shape");
    }
}

You can create any Shape like this and compare it.

You need to look for touch events (eg TouchesBegan, TouchesMoved, TouchesEnded, TouchesCancelled) in your view. When a touch occurs, you can get the location of the touch in your view. You can use this location and check if that point is inside your path.

See this post: Path Interaction

How views are drawn (your bezier path) is not much related to how they are interacted with user (touches).

I think you should implement -pointInside:withEvent: in your UIView subclass, this method determine if a touch event is inside a view. You should return NO when the point should not be touchable.

Methods like -touchesBegan: are also work, but a little more complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM