2012-04-14 10 views
8

그리기 rect를 지나치게 거미 차트를 만들었습니다. 내 영역을 그리기 위해 코어 grahics CAShapeLayer를 사용하고 있습니다. 화면에 여러 개의 CAShapeLayer 영역이 만들어져 있는데, 어떤 레이어가 터치되었는지 감지하고 싶습니다. 사용자가 접촉 ...하지만 어떻게 알아낼 수 없다?CAShapeLayer 터치 감지

답변

16

먼저 drawRect에서 도면층을 그려서는 안되지만 질문은 아닙니다. 당신은 단지 더 많은 빛,의 경우 조건 내부의 코드를 확장 할 수 있습니다, 당신은 같은 것을 할 수 있습니다 "감동"하는 레이어 ...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 
     CGPoint touchLocation = [touch locationInView:self.view]; 
     for (id sublayer in self.view.layer.sublayers) { 
      BOOL touchInLayer = NO; 
      if ([sublayer isKindOfClass:[CAShapeLayer class]]) { 
       CAShapeLayer *shapeLayer = sublayer; 
       if (CGPathContainsPoint(shapeLayer.path, 0, touchLocation, YES)) { 
        // This touch is in this shape layer 
        touchInLayer = YES; 
       } 
      } else { 
       CALayer *layer = sublayer; 
       if (CGRectContainsPoint(layer.frame, touchLocation)) { 
        // Touch is in this rectangular layer 
        touchInLayer = YES; 
       } 
      } 
     } 
    } 
} 
+0

안녕 조디를 확인하려면 – user1333444

+0

OK, 나는 조금으로 업데이트 자세한 세부 사항. 기본적으로, 그것이 shape 레이어라면, 당신은 패스를 쿼리하고 그 패스가 포인트를 포함하고 있는지를 보게됩니다.하지만, CGPathContainsPoint의 일부로 fill-type을 전달해야한다는 것을 기억하십시오. - 나는 짝수/홀수로 가정합니다. 당신이 필요로하는 것을 사용하십시오 ... –

+0

선 너비가 1보다 큰 경우 테두리에 건 드리면 작동하지 않습니다. – jjxtra