2014-10-08 55 views
1

嗨,我画了一条线,并检测线被触摸“触摸移动”。 这工作不错,但黄色的部分被取为线 enter image description here绘制没有fillColor的CAShapeLayer层

我需要删除图像的黄色部分..... 这是一个被称为组件的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"); 
     } 
    } 
} 

我已经尝试设置为零,颜色并没有什么表示,只是红线,这是正确的,但触摸移动,返回真会在哪里黄色部分.... 是否有可能删除或初始化该行..没有这部分图? 在此先感谢

+0

阅读更多关于这一切请注明您的代码的触摸操作部分;目前尚不清楚你遇到的问题是什么。 – 2014-10-08 18:30:44

+0

嗨,诺亚·威瑟斯彭,我只是包括触摸事件被称为的“touchesMoved”事件 – lyons 2014-10-08 19:28:51

+0

我的问题是,黄色的部分被检测为图 的一部分,尽管分配给空将fillColor – lyons 2014-10-08 19:38:30

回答

3

你正在绘制路径的方式与它被撞击测试的方式无关。当您检查路径是否包含点时,即使您在绘制路径时没有填充颜色,它也会检查点是否在填充区域内。

相反,您需要做的是生成一个新的路径,其中填充区域是描边路径。您可以通过调用CGPathCreateCopyByStrokingPath()为此核芯显卡(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

+0

谢谢大卫Rönnqvist,将尝试你的例子现在 谢谢! – lyons 2014-10-09 12:48:53