最前面可见的UIView没有接触,为什么?

时间:2014-01-23 11:03:42

标签: ios objective-c uiview uitouch

考虑以下视图层次结构

  • self.view
    • self.transformedView(z-transform = 10,index 0)
    • self.ordinaryView(z-transform = 0,index 1)

通过此代码实现生命

@interface AGSTransformedView : UIView
@end
@implementation AGSTransformedView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"The transformed view is receiving touch");
    [super touchesBegan:touches withEvent:event];
}

@end


@interface AGSOrdinaryView : UIView
@end
@implementation AGSOrdinaryView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"The ordinary view is receiving touch");
    [super touchesBegan:touches withEvent:event];
}

@end

@interface AGSViewController ()

@end

@implementation AGSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    AGSTransformedView *transformed = [[AGSTransformedView alloc] initWithFrame:self.view.bounds];
    transformed.layer.transform = CATransform3DMakeTranslation(0, 0, 10);
    transformed.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:transformed];

    AGSOrdinaryView *ordinary = [[AGSOrdinaryView alloc] initWithFrame:self.view.bounds];
    ordinary.backgroundColor = [UIColor blueColor];
    [self.view addSubview:ordinary];
}

@end

点击屏幕时,我在控制台中看到了这个

The ordinary view is receiving touch

在屏幕上我只看到黄色视图(变换视图)。

为什么最前面的可见视图不会接触到触摸?

1 个答案:

答案 0 :(得分:1)

似乎UIKit在确定哪个视图应该是触摸接收器时仅尊重视图层次结构。我还没有找到这方面的文件。