2012-06-21 51 views
2

我目前正在尝试创建一个视图,以处理可能发生在我的应用程序上的所有手势。查看透明度和手势处理

我想这个观点透明才能把下面另一种观点,他们仍然会显示(我不想让他们处理视图的subbiews)

的手势操作正常工作,直到我设置然后从“clearColor”上查看颜色,它已经消失。除非我坚持子视图,在这种情况下,手势只发生在点击子视图时。

因此,我的问题是:我如何设法让手势事件发生在透明视图上?

+0

如果没记错,具有不透明性的视图设置为0,将不会对手势做出反应。不过,我希望能够做到这一点以及.. – Patrick

+0

您是否透明地查看手势的委托?我不确定您是在故事板还是在代码中执行此操作,但在故事板中,例如,是否将控件从手势识别器拖动到透明视图并将其设置为委托?认为这应该工作。 –

+0

请参阅:http://stackoverflow.com/questions/3344341/uibutton-ins ide-a-view-that-has-a-uitapgesturerecognizer – Patrick

回答

1

尝试类似这样的事情。这段代码在主视图中添加了两个子视图“bottomView有一个红色的背景,然后是”testView“,它是一个透明的覆盖”bottomView“顶部的轻击手势识别器。如果你点击”testView“打印出NSLog的消息。我希望帮助。

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    [bottomView setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:bottomView];  
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)]; 
    [testView setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:testView]; 

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(handleTap:)]; 
    [tapRecognizer setNumberOfTapsRequired:1]; 
    [tapRecognizer setDelegate:testView]; 
    [testView addGestureRecognizer:tapRecognizer]; 
} 

-(void)handleTap:(UITapGestureRecognizer *)sender 
{ 
    NSLog(@"Tapped Subview"); 
} 
+0

非常感谢,工作正常。 – boubou

+0

如何让testView成为tapRecognizer的代表?不应该是处理viewDidLoad的viewController? – tiguero

+1

我也想提一下,这个解决方案似乎不适用于透明视图。你可否确认? – tiguero