title: iOS CALayer and TapGestureRecognizer
body: <p>I'm developing an app on iOS 6.1 for iPad. 
  I've a problem with the <code>CALayer</code> and a <code>TapGestureRecognizer</code>.</p>
  
  <p>I've 7 <code>CALayers</code> forming a rainbow (every layer is a colour). 
  Every layer is built using a <code>CAShapeLayer</code> generate from a <code>CGMutablePathRef</code>. Everything works fine. All the layers are drawn on screen and I can see a beautiful rainbow.</p>
  
  <p>Now I want to detect the tap above a single color/layer. I try this way:</p>
  
  <pre><code>- (void)tap:(UITapGestureRecognizer*)recognizer
  {
     //I've had the tapGestureRecognizer to rainbowView (that is an UIView) in viewDidLoad
    CGLayer* tappedLayer = [rainbowView.layer.presentationlayer hitTest:[recognizer locationInView:rainbowView];
  
    if (tappedLayer == purpleLayer) //for example
           NSLog(@"PURPLE!");
  }
  </code></pre>
  
  <p>I don't understand why this code won't work! I've already red other topics in here: all suggest the <code>hitTest:</code> method for solving problems like this. But in my case I can't obtain the desired result. 
  Can anyone help me? Thanks!!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's the code for the creation of paths and layers:</p>
  
  <pre><code>- (void)viewDidLoad
  {
       //Other layers
       ...
       ...
       //Purple Arc
       purplePath = CGPathCreateMutable();
       CGPathMoveToPoint(purplePath, NULL, 150, 400);
       CGPathAddCurveToPoint(purplePath, NULL, 150, 162, 550, 162, 550, 400);
       purpletrack = [CAShapeLayer layer];
       purpletrack.path = purplePath;
       purpletrack.strokeColor = [UIColor colorWithRed:134.0/255.0f green:50.0/255.0f blue:140.0/255.0f alpha:1.0].CGColor;
       purpletrack.fillColor = nil;
       purpletrack.lineWidth = 25.0;
       [rainbowView.layer insertSublayer:purpletrack above:bluetrack];
  }
  </code></pre>
  
  <p>This was my first approach to the problem. And the touch didn't work.</p>
  
  <p>I also tried to create a <code>RainbowView</code> class where the rainbow was drawing in <code>drawRect</code> method using <code>UIBezierPaths</code>.
  Then I follow the "Doing Hit-Detection on a Path" section in <a href="http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html" rel="nofollow noreferrer">http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html</a>
  In this case the problem was the path variable passed to the method. I try to compare the <code>UIBezierPath</code> passed with the paths in <code>RainbowView</code> but with no results.</p>
  
  <p>I could try to create curves instead of paths. In this case maybe there isn't a fill part of figure and the touching area is limited to the stroke. But then how can I recognize the touch on a curve?</p>
  
  <p>I'm so confused about all of these stuff!!! :D</p>