0

I'm just starting out learning about iOS GUI after playing around with backend stuff and Objective-C in general the last few weeks. I've stumbled upon a tricky situation that I've managed to solve but I want to know if there is a better way to solve it.

Here's how I've layered my UIViews:

[self.view.layer insertSublayer:_topView.layer above:self.view.layer];
[self.view.layer insertSublayer:_bottomView.layer above:self.view.layer];

So I have the "base" view and a single ViewController. In this view I have two subviews: topView and bottomView. I have been able to manipulate the origin of these views just like I want but even when they are in front of my UIButton it reacts to touch.

I solved this by checking the y axis of the origin of one of the subviews (it doesn't matter which one) to make sure that the button is revealed and thus activate it (or enable user interaction, rather).

if (_topView.frame.origin.y == 0) {
    self.refreshButton.userInteractionEnabled = NO;
} else {
    self.refreshButton.userInteractionEnabled = YES;
}

I personally dislike this solution and would like to know what a better way to solve this is.

Can anyone point me in the right direction so that I get this layer business going without ugly hacks here and there?

I am using storyboards with Xcode 5.0.

Cheers!

4

1 回答 1

1

插入子层根本不会影响触摸转发。视图的子视图(在您的情况下为按钮)只能通过在其上添加子视图来“覆盖”。如果您没有使用CALayers 的特殊原因,则只需添加子视图,然后它将以正确的方式处理触摸。像这样:

[self.view insertSubview:_topView aboveSubview:self.refreshButton];
[self.view insertSubview:_bottomView aboveSubview:self.refreshButton];
于 2013-10-22T13:31:28.493 回答