2012-06-21 50 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