2012-07-19 27 views
2

所以我有以下代碼:添加一個手勢識別到幾UIView的

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)]; 
[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[showNewsStoryTapGestureRecognizer release]; 

看來,對於一個UIView這隻作品,這是最後一個加入。換句話說UITapGestureRecognizer,它的觀點是一對一的關係。它是否正確?我該如何解決?我必須爲每個單獨創建一個UITapGestureRecog嗎?

+1

你需要爲每個單獨 – 2012-07-19 06:03:25

回答

5

是的,只能有一個UITapRecogniser一個UIView。儘管他們的行爲可能相同,但您必須針對不同的觀點採用不同的識別器。
另請參閱this鏈接。

0

試試這個,

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)]; 

[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[showNewsStoryTapGestureRecognizer release]; 

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)]; 
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[showNewsStoryTapGestureRecognizer release]; 

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)]; 
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[showNewsStoryTapGestureRecognizer release]; 

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)]; 
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer]; 
[showNewsStoryTapGestureRecognizer release]; 
+0

UITapGesture您不添加相同'UITapGestureRecognizer'不同的意見。你總是爲每個UIView創建一個新的: 'initWithTarget'初始化一個新的'UITapGestureRecognizer'實例。 – Fabio 2014-06-17 13:35:10

0

我認爲您只需將手勢識別器添加到包含您的storyImageView,storyTitleLabel等視圖作爲其子視圖的視圖。

-1

您可以使用此代碼將多個Same UITapGestureRecognizer添加到多個View中。

步驟是:

  1. 首先,我們創建三個視圖與標籤
  2. 然後我們創建NSMutableArray並添加該視圖陣列
  3. 之後添加UITapGestureRecognizer查看
  4. 在UITapGestureRecognizer方法我們檢查視圖標記來區分哪個視圖被點擊。

下面是步驟的代碼:

-(Void)viewDidLoad { 
    [super viewDidLoad]; 

    //First create three View 
    UIView *view1 = [[UIView alloc] initWithFrame: CGRectMake (5 , 171, 152, 152)];  
    view1.tag = 1; //add tag to view 
    view1.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview: view1]; 

    UIView * view2 = [[UIView alloc] initWithFrame: CGRectMake (163, 171, 152, 152)]; 
    view2.tag = 2; //add tag to view 
    view2.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview: view2]; 

    UIView * view3 = [[UIView alloc] initWithFrame: CGRectMake (5, 330, 152, 152)];  
    view2.tag = 3; //add tag to view 
    view2.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview: view2]; 

    //Now create mutable array to hold our view 
    NSMutableArray * ary=[[NSMutableArray alloc] init]; 
    [ary addObject:view1]; 
    [ary addObject:view2]; 
    [ary addObject:view3]; 


    //now we add tap gesture to view 
    for (UIView *view in ary) { 
     UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(answerDoubleTapped:)]; 
     answerDoubleTapGesture.numberOfTapsRequired = 2; 
     [answer4View addGestureRecognizer:answerDoubleTapGesture]; 
    } 
    } 

-(void)answerDoubleTapped:(UITapGestureRecognizer *)recognizer { 
    //Check which view is tapped 
    switch (recognizer.view.tag) { 
     case 1: 
     { 
      NSLog(@"First View Tapped"); 
      break; 
     }case 2: 
     { 
      NSLog(@"Second View Tapped"); 
      break; 
     }case 3: 
     { 
      NSLog(@"Third View Tapped"); 
      break; 
     }case 4: 
     { 
      NSLog(@"Forth View Tapped"); 
      break; 
     }default: 
     { 
      break; 
     } 
    } 
} 
+1

你不會將SAME UITapGestureRecognizer添加到不同的視圖。你總是爲每個UIView的創造新的: 'UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer頁頭] initWithTarget:自我行動:@選擇(answerDoubleTapped :)]' 'initWithTarget'初始化UITapGestureRecognizer'的'一個新的實例。 – Fabio 2014-06-17 13:32:01