1

我有6個不同的視圖,我給他們標籤爲1,2,3,4,5,6。我將UITapGestureRecogniser從對象庫添加到故事板,並通過CTRL +從視圖中拖動到UITapGestureRecogniser來製作所有6個視圖gestureRecognizers。 現在在下面的方法中,我試圖通過點擊不同的視圖來獲取標籤。UI Tap Gesture識別器不工作不知道是哪個視圖被點擊

- (IBAction)colorTapRecognizer:(UITapGestureRecognizer *)sender { 
     UIView *tappedView= sender.view; 
     NSLog(@"%d",tappedView.tag ); 

    } 

但它總是顯示我相同的標記。即,如果我點擊並且第一次是3,則通過點擊其他視圖也是如此。

+0

你在哪添加的?請提供一些代碼。 – 2014-10-30 06:55:28

+0

我從對象庫中添加它。我實際上把它放在其中一個視圖上,然後只是CTRL +將其他人拖到底欄上的「自頂向下手勢識別器」 – 2014-10-30 07:05:37

+3

您必須爲每個視圖使用不同的手勢識別器 – jcesarmobile 2014-10-30 07:54:51

回答

3

每個視圖都需要自己的手勢識別器集。這是一段適合你的代碼。

-(void)addTapGesturesToViews 
{ 
    int maxViewTag = 6; // tags must be consective (are in your case) 
    for(int i =1; i<= maxViewTag;i++) 
    { 
     UIView * view = [self.view viewWithTag:i]; 
     UITapGestureRecognizer * tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(colorTapRecognizer:)]; 
     tapGest.numberOfTapsRequired = 1; 
     [view addGestureRecognizer:tapGest]; 
    } 

} 
相關問題