2014-10-08 21 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