2010-08-05 38 views
1

我有一個包含UITableView(subclassed)和另一個UIView(subclassed)的UIViewController。它們處於同一層次結構中,但最後添加了UIView,因此它是最前面的。 我覆蓋touchesBegan/Moved /結束從頂部UIView攔截手勢:我的目標是獲得選定的UITableViewCell,如果雙擊,創建一個ImageView被拖動。 我似乎完成了它,但現在我無法滾動UITableView,即使我轉發觸摸事件。 下面是UIView的方法:UITableView:自定義手勢使它不再滾動

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"BO"); 
    UITouch * touch = [touches anyObject]; 

    CGPoint tPoint = [touch locationInView:self]; 
    InventoryViewController * invViewCont = self.viewController; 
    UITableView * invTab = invViewCont.inventoryTableView; 
    [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
          animated:YES]; 
    NSArray * cells = [invTab visibleCells]; 
    BOOL found = NO; 
    for (UITableViewCell * cell in cells) 
    { 
     if (CGRectContainsPoint(cell.frame, tPoint)) 
     { 
      [cell touchesBegan:touches withEvent:event]; 
      found = YES; 
      break; 
     } 
    } 


    if (!found) 
    { 
     [invViewCont.inventoryTableView touchesBegan:touches withEvent:event]; 
    } 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
     NSLog(@"Mo"); 
     UITouch * touch = [touches anyObject]; 
     CGPoint tPoint = [touch locationInView:self]; 

     copyObj.center = tPoint; 
     InventoryViewController * invViewCont = self.viewController; 
     UITableView * invTab = invViewCont.inventoryTableView; 

     [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
           animated:YES]; 
     NSArray * cells = [invTab visibleCells]; 
     BOOL found = NO; 
     for (UITableViewCell * cell in cells) 
     { 
      if (CGRectContainsPoint(cell.frame, tPoint)) 
      { 
       [cell touchesMoved:touches withEvent:event]; 
       found = YES; 
       break; 
      } 
     } 

     if (!found) 
     { 
      [invViewCont.inventoryTableView touchesMoved:touches withEvent:event]; 
     } 

    } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

     UITouch * touch = [touches anyObject]; 
     if ([touch tapCount] == 2) 
     { 
      [self desubCopyView]; 
     } 
     CGPoint tPoint = [touch locationInView:self]; 
     copyObj.center = tPoint; 
     InventoryViewController * invViewCont = self.viewController; 
     UITableView * invTab = invViewCont.inventoryTableView; 
     [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
           animated:YES]; 
     NSArray * cells = [invTab visibleCells]; 
     BOOL found = NO; 
     for (UITableViewCell * cell in cells) 
     { 
      if (CGRectContainsPoint(cell.frame, tPoint)) 
      { 
       [cell touchesEnded:touches withEvent:event]; 
       //[cell.imageView touchesEnded:touches withEvent:event]; 
       found = YES; 
       break; 
      } 
     } 
     if (!found) 
     { 
      [invViewCont.inventoryTableView touchesEnded:touches withEvent:event]; 
     } 
    } 

這裏是指那些在UITableViewCell中

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch * touch = [touches anyObject]; 
    if ([touch tapCount] == 2) 
    { 
     CGPoint tPoint = [touch locationInView:self]; 
     NSLog(@"CellX %lf CY %lf", tPoint.x, tPoint.y); 

     UIGraphicsBeginImageContext(self.bounds.size); 
     [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     UIImageView * newView = [[UIImageView alloc] initWithImage:viewImage]; 
     [dragArea addSubview:newView]; 
     dragArea.copyObj = newView; 
     [newView release]; 

     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.4]; 
     dragArea.copyObj.transform = CGAffineTransformMakeScale(1.3, 1.3); 
     [UIView commitAnimations]; 
     tPoint = [self convertPoint:tPoint toView:dragArea]; 
     dragArea.copyObj.center = tPoint; 
    } 
    [super touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"MOV %@", self.imageView.image); 
    [super touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"ENDED"); 
    [super touchesEnded:touches withEvent:event]; 
} 

在我的UITableView我乾脆:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"BEGTB"); 
    [super touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"MOVTB"); 
    [super touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
     NSLog(@"ENDTB"); 
    [super touchesEnded:touches withEvent:event]; 
} 

我肯定失去了一些東西但我不知道什麼

回答

0

我找到了解決方法,重寫了我的自定義UITableView中的觸摸手勢的方法,以便在我拖動對象時使其滾動programMatically。 Here是'解決方案'。

我仍然認爲還有另一種更簡單的方法來做到這一點,但我沒有找到它,所以張貼此標記並將其標記爲「答案」可能有助於其他人。

0

我強烈建議調查UITapGes tureRecognizer而不是自己處理touchesX事件。

+0

雙擊後,我需要識別用戶,而他拖動我創建的新視圖,是否只有該UIGestureRecognizer可能? – rano 2010-08-05 19:58:34

+0

我相信如果您添加了UIPanGestureRecognizer,您將能夠檢測到它們拖動它。 – BarrettJ 2010-08-05 21:01:57

+0

第二次閱讀時,我不確定你想要的是用識別器實現的 - 是你想要用戶點擊一次,然後在第二次點擊時用戶將手指放下,並創建圖像視圖並拖動或者是用戶雙擊,然後創建圖像視圖,第三次用戶放下手指時,圖像視圖將被拖動? – BarrettJ 2010-08-05 21:03:39