不使用fillColor绘制CAShapeLayer LAYER

时间:2014-10-08 18:21:24

标签: objective-c sprite-kit core-animation

嗨我画了一条线并检测线是否触及“触摸移动”。 这样工作正常,但黄色部分被视为该行的一部分 enter image description here

我需要删除图像的黄色部分..... 这是一个名为fillColor的组件属性,我将此属性赋值为nil,但仍被视为该行的一部分

这是我正在处理的代码:

    self.path                = [UIBezierPath bezierPath];
    [self.path moveToPoint:CGPointMake(10, 150)];
    [self.path addCurveToPoint:CGPointMake(110, 150) controlPoint1:CGPointMake(40,  100)    controlPoint2:CGPointMake(80,  100)];
    [self.path addCurveToPoint:CGPointMake(210, 150) controlPoint1:CGPointMake(140, 200) controlPoint2:CGPointMake(170, 200)];
    [self.path addCurveToPoint:CGPointMake(310, 150) controlPoint1:CGPointMake(250, 100) controlPoint2:CGPointMake(280, 100)];
    //[self.path addCurveToPoint:CGPointMake(310, 150) controlPoint1:CGPointMake(250, 100) controlPoint2:CGPointMake(280, 100)];

     self.layer               = [CAShapeLayer layer];
     self.layer.lineWidth     = 10;
     self.layer.strokeColor   = [UIColor redColor].CGColor;
     self.layer.fillColor     = [UIColor yellowColor].CGColor;
     self.layer.path          = self.path.CGPath;
     self.layer.shadowOffset  = CGSizeZero;
     self.layer.lineCap       = kCALineCapRound;
     self.layer.fillRule      = @"non-zero";
     [self.view.layer addSublayer: self.layer];

此属性:

         self.layer.fillColor     = [UIColor yellowColor].CGColor; 

触摸事件:

-(void)DetectTouchedDraw :(NSSet *)touches withEvent:(UIEvent *)event
{
     for (UITouch *touch in touches)
     {
         CGPoint touchLocation = [touch locationInView:self.view];

         if ([self.path containsPoint:touchLocation]) {

             NSLog(@": %@",@"Touched");
         }
     }
}

我已经尝试设置为nil,颜色并没有显示任何内容,只有红线,这是正确的,但触摸的移动返回true,其中黄色部分.... 是否可以删除或初始化该行..没有图的这一部分? 提前致谢

1 个答案:

答案 0 :(得分:3)

您绘制路径的方式与其受到测试的方式无关。当您检查路径是否包含一个点时,它将检查该点是否在填充区域内,即使您在绘制路径时没有填充颜色。

相反,您需要做的是生成一个新路径,其中填充区域是描边路径。您可以通过调用CGPathCreateCopyByStrokingPath()在Core Graphics(CGPath而不是UIBezierPath)中执行此操作:

// Create a new path by stroking the Bézier path
// Note: since you are drawing the path using a shape layer you should get the appearance from there 
CGPathRef tapTargetPath = 
    CGPathCreateCopyByStrokingPath(yourUIBezierPath.CGPath,
                                   NULL, // don't transform the path
                                   fmaxf(35.0, yourShapeLayer.lineWidth), // if the path is thin you probably want a thicker stroke (like 35 points) for hit testing
                                   yourShapeLayer.lineCap,
                                   yourShapeLayer.lineJoin, 
                                   yourShapeLayer.miterLimit);

您可以在Ole Begemman's CGPath Hit Testing post

中详细了解所有这些内容