1

我有一个或多或少具有弧形的 BezierPath,问题是我希望用户能够在弧内点击(它可能是更清晰的门)但不是在弧上. 但我不能这样做,就像弧内的空间也是 BezierPath 的一部分,但唯一的是它是透明的。

创建 BezierPath 的代码如下:

- (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;
}

在这种方法中,我创建了 bezierPath 并将其保存在 UIImageView 中,以便将其添加到另一个视图中。

我以为 bezierPath 用于绘制奇怪的表格并只占用该表格的空间,但现在我不知道该怎么想!

有什么想法吗?谢谢你!

4

3 回答 3

3

第 1 步:添加QuartzCore.Framwork您的项目

第 2 步:#import <QuartzCore/QuartzCore.h>在您的 .h 文件中

UIBezierPath *path;第 3 步:在 .h 文件中定义一个变量

viewDidLoad第 4 步:在您的方法中绘制三角形

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];

第 5 步:实现以下触摸方法来检测视图和形状上的触摸

-(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");
    }
}

您可以像这样创建任何形状并进行比较。

于 2013-04-25T19:15:03.183 回答
0

如何绘制视图(您的贝塞尔路径)与它们如何与用户交互(触摸)没有太大关系。

我认为您应该-pointInside:withEvent:在您的UIView子类中实现,此方法确定触摸事件是否在视图内。NO当该点不可触摸时,您应该返回。

类似-touchesBegan:的方法也可以使用,但稍微复杂一些。

于 2013-04-25T19:42:41.657 回答
0

您需要在视图中查找触摸事件(例如 TouchesBegan、TouchesMoved、TouchesEnded、TouchesCancelled)。当触摸发生时,您可以在视图中获取触摸的位置。您可以使用此位置并检查该点是否在您的路径内。

看到这篇文章:路径交互

于 2013-04-24T23:26:51.870 回答