views:

57

answers:

1

I have a UIScrollView which contains any number of thumbnails that should detect the touch event, perform 3D transformation on themselves and call a delegate method.

The one problem I am unable to overcome is detecting which sublayer is being tapped. I am setting layer.name to an index count and the subclass of the UIScrollView does get the touch event. Now the last remaining obstacle is how to do a hitTest on the sublayers to get the name property and voila!

I was thinking of subclassing a UIView as the container of the CALayer and capturing the touch event that way. But I am hoping there might be a more economical way to determine the sublayer being touched.

The code I have been trying (in my UIScrollView subclass):

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 1) {
        for (UITouch *touch in touches) {

            CGPoint point = [touch locationInView:[touch view]];
            CALayer *alayer = [self.layer hitTest:point];
            if (alayer) NSLog(@"layer %@ touched",alayer.name);
            else NSLog(@"layer not detected");

        } 
    } 
}

I am getting two compile errors. first a warning: no hitTest method found. and an error "request for member 'name' is something not a structure or union".

+1  A: 

Call [scrollView.layer hitTest:touchPoint];. The return value will be the layer that was touched. If the returned layer is equal to scrollView.layer, no sublayer was tapped.

Ole Begemann
Ole, I was aiming for the same solution, I think. However, I am getting two compile errors with my code which I posted above.
cameron
You probably didn't `#import <QuartzCore/QuartzCore.h>`.
Ole Begemann
You are correct, sir! :x
cameron