2012-08-26 45 views
1

enter image description here识别关于3D立方体

该立方体是由6个CALayers其然后添加到单个CATransformLayer的特定侧触摸。 这个transformLayer - 使用它,立方体 - 可以使用触摸输入进行旋转。

问题:我希望能够识别用户触摸立方体的哪一侧。

我曾尝试这样的代码:

- (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"); 
    } 

但立方体似乎在2D空间映射:当我触碰面1,不仅1侧的认可,而且其背后的一侧(例如侧4)。

我该如何确保只选择离用户较近的一侧(较高的z坐标)?

回答

0

您是否尝试过在两侧的表示层上使用hitTest方法? 又名沿线:

CALayer* touchedLayer = [[self.side4.presentationLayer hitTest:location] modelLayer]; 
if(touchedLayer!=nil){ 
    NSLog(@"Side 4"); 
} 

想法?

+0

不,它仍然'检测'双方每次点击 –