2012-03-20 93 views
0

我的代碼如下所示:手勢識別似乎不工作

- (void)setPosts:(NSArray *)posts 
{ 
    _posts = posts; 


    int totalHeight = 0; 
    for (TumblrPost *post in posts) { 
     totalHeight += post.thumbH; 
    } 
dispatch_queue_t mainQ = dispatch_get_main_queue(); 
dispatch_async(mainQ, ^{ 
    int cumulativeY = 0; 
    int postCount = 0; 
    for (TumblrPost *post in posts) { 
     NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"ThumbnailView" owner:nil options:nil];    
     ThumbnailView* thumbnail = [array objectAtIndex:0]; 
     thumbnail.frame = CGRectMake(0,cumulativeY,0,0); 
     thumbnail.userInteractionEnabled = YES; 

     UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMainImage:)]; 
     [thumbnail addGestureRecognizer:gesture]; 

     [self.multiThumbnailView addSubview:thumbnail]; 
     [thumbnail loadUrl:post.url]; 

     cumulativeY+=100;//post.thumbH; 
     if(postCount >=2) 
      break; 
     postCount++; 
    } 
    NSLog(@"Set posts method"); 
}); 
} 

- (void)showMainImage:(UITapGestureRecognizer *)gesture 
{ 
    if(gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) 
    { 
     int thumbIndex = [self.multiThumbnailView.subviews indexOfObject:gesture.view]; 
     self.selectedPost = (TumblrPost*)[self.posts objectAtIndex:thumbIndex]; 

     [self performSegueWithIdentifier:@"ShowMainPost" sender:self]; 
    } 
} 

multiThumbnailView是一個UIView,我有我的故事板和ThumbnailView是廈門國際銀行/類組合這是一個100×100正方形裏面有一個標籤,裏面寫着'測試'。

當我運行我的代碼時,我得到了三個垂直線框,但當我單擊我的子視圖時,手勢識別器不會觸發。一切都有userInteractionEnabled打勾。我試圖在主multiThumbnailView上做一個測試gesturerecognizer,並且工作。

請幫忙!

+0

我編輯我的答案與另一個選項 – 2012-03-20 11:28:22

回答

4

我的猜測是ThumbnailViewUIImageView的一個子類 - 它默認將其userInteractionEnabled設置爲NO

確保您在每個ThumbnailView上設置userInteractionEnabled = YES您想截取水龍頭。

編輯:

此外,還設置ThumbnailView的幀以與(0,0)的尺寸。 這意味着這個視圖基本上是不可見的,因此不會攔截觸摸。

最後,請不要做:

ThumbnailView* thumbnail = [array objectAtIndex:0]; 

相反,你可以檢查數組或簡單的計數:

ThumbnailView* thumbnail = [array lastObject]; 
+0

嗨,謝謝參觀。 ThumbnailView是UIView的一個子類,並且有一個關聯的xib文件,我在Xcode中使用'Custom Class'來鏈接。它在Xcode中打勾了User Interaction Enabled。我也將其設置在上面的代碼中。 – mikeydelamonde 2012-03-20 11:22:40

+0

啊!你是一個天才 - 非常感謝你! – mikeydelamonde 2012-03-20 11:34:27