The cube is made of 6 CALayers which are then added to a single CATransformLayer. This transformLayer -and with it, the cube- are rotatable using touch input.
Problem: I want to be able to recognize which side of the cube is touched by the user.
I have tried this code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint location = [[touches anyObject] locationInView:self];
if ([self.side1 containsPoint: [self.layer convertPoint:location toLayer:self.side1]]) {
NSLog(@"Side 1");}
if ([self.side2 containsPoint: [self.layer convertPoint:location toLayer:self.side2]]) {
NSLog(@"Side 2"); }
if ([self.side3 containsPoint: [self.layer convertPoint:location toLayer:self.side3]]) {
NSLog(@"Side 3 "); }
if ([self.side4 containsPoint: [self.layer convertPoint:location toLayer:self.side4]]) {
NSLog(@"Side 4"); }
if ([self.side5 containsPoint: [self.layer convertPoint:location toLayer:self.side5]]) {
NSLog(@"Side 5"); }
if ([self.side6 containsPoint: [self.layer convertPoint:location toLayer:self.side6]]) {
NSLog(@"Side 6");
}
But the cube seems to be mapped on a 2D space: when i touch side 1, not only side 1 is recognized but also the side behind it (e.g. side 4).
how can I make sure that only the side which is nearer to the user (higher z coordinate) is selected?
Have you tried using the hitTest method on the presentation layer of the sides? aka something along the lines of:
Thoughts?