views:

1575

answers:

2

ok -

I'm new to the iPhone SDK. Right now I'm programming with CALayers which I really like a lot- not as expensive as UIViews, and a lot less code than OpenGL ES sprites.

I have this question- is it possible to get a touch event on a CALayer? I understand how to get a touch event on a UIView with

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

but I can't find anywhere about how to get a touch event on a CALayer object, for instance, an orange square floating in 3D space. I refuse to believe I'm the only one who's curious about this.

I appreciate any help!

+7  A: 

ok- answered my own question! let's say you've got a bunch of CALayers in your view controller's main layer, and you want them to go to opacity 0.5 when you touch them. implement this code in the .m file of your view controller class:

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

CGPoint point = [touch locationInView:[touch view]]; point = [[touch view] convertPoint:point toView:nil];

CALayer *layer = [(CALayer *)self.view.layer.presentationLayer hitTest:point];

layer = layer.modelLayer; layer.opacity = 0.5;

} } }

powerrrrr!! unlimited powweerrrr!

A: 

Hey Alex,

You seem like a good person to ask. I haven't come across any threads relating to differentiating CALayers that are touched. I am curious how you would go about doing it?

For example, I have two CALayers on a main view. I want to change the color of the one that is touched, and if it is touched again, revert to the original color. But I can't seem to figure out how to tell the difference between two CALayers in - (void)touchesBegan:

Any insight you have would be awesome.

Thank you!

  • Kagi
YuKagi