使用图层创建具有可点击边的3D立方体 - 目标C.

时间:2013-11-02 13:37:39

标签: ios objective-c 3d uibutton calayer

我现在正在尝试创建一个带有“3D立方体”和可点击边的应用程序。

为了创建一个'3D立方体',我发现了一篇很棒的文章:here 它使用了一些“层”。

我想做的是在3D中完成一个立方体,但是有可点击的边。我发现了一些像这样的例子:

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

但是这种代码在不同方面没有区别。所以我尝试对文章中的代码进行一些更改。例如,要创建第一面,代码为:

CALayer *sideOne = [CALayer layer];
sideOne.borderColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
sideOne.backgroundColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:0.4].CGColor;
sideOne.borderWidth = Border_Width;
sideOne.cornerRadius = Corner_Radius;
sideOne.frame = layerRect;
sideOne.position = self.center;

和:

transformLayer = [CATransformLayer layer];
[transformLayer addSublayer:test.layer];
transformLayer.anchorPointZ = Layer_Size/-2;
[rootLayer addSublayer:transformLayer];

所以我尝试做一些修改,最后在这一边添加'UIButton'。像这样:

UIButton * test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setFrame:CGRectMake(0.0, 0.0, layerSize, layerSize)];
[test addTarget:self action:@selector(ActionTest) forControlEvents:UIControlEventTouchUpInside];
test.layer.borderColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
test.layer.backgroundColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:0.4].CGColor;
test.layer.borderWidth = Border_Width;
test.layer.cornerRadius = Corner_Radius;
test.layer.frame = layerRect;
test.layer.position = self.center;

但问题是,当我想将它添加到一边时。

当我这样做时:

transformLayer = [CATransformLayer layer];
[self addSubview:test];
[transformLayer addSublayer:test.layer];
transformLayer.anchorPointZ = Layer_Size/-2;
[rootLayer addSublayer:transformLayer];

按钮无反应

当我这样做时:

transformLayer = [CATransformLayer layer];
[transformLayer addSublayer:test.layer];
transformLayer.anchorPointZ = Layer_Size/-2;
[self addSubview:test];
[rootLayer addSublayer:transformLayer];

按钮会做出反应,但不会随着立方体的其余部分移动

我理解这个问题,但不知道如何修复它......

谢谢!

1 个答案:

答案 0 :(得分:1)

你所看到的是从UIResponder继承的UIButton与不直接构成响应者链的一部分的CALayer之间的区别。

幸运的是,有一种简单的方法可以从响应链中获取事件并将它们引向您选择的CALayer,它看起来有点像这样:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint p = [(UITouch*)[touches anyObject] locationInView:self.worldView];
    for (CALayer *layer in self.worldView.layer.sublayers) {
        if ([layer containsPoint:[self.worldView.layer convertPoint:p toLayer:layer]]) {
            // do something
        }
    }
}

这是containsPoint:convertPoint:toLayer:,它将保留你所寻求的本质。

a bit more largely relevant discussion around this topic on this post too

要将此点连接到您的调用,当您将按钮的图层添加到其他图层时,所有可视方面都按预期执行,但没有人是响应者链的成员,因此没有人接收到处理