2012-04-30 109 views
3

我有一個動態表格視圖,其中包含從單元格中填充的單元格。 當一個單元格被選中時,用戶應該有可能選擇其他幾個選項。 我知道如何在選中單元格時推送另一個視圖,但我不太喜歡這種方法。 例如,如果同一個單元格可以翻轉並用滑動顯示選項(然後翻轉),那可能會更好。 或者整個單元格可能會從屏幕上滑下來顯示選項,或者另一個視圖可能會從單元格向下滑動,然後向後滑動。將滑動視圖添加到表格單元格時

哪種解決方案最容易做到? 任何人都可以指向正確的方向嗎?我當然不需要代碼,我在這裏學習,我只需要知道要看什麼。直到現在,我已經讀了一些關於繼承UITableViewCell的東西,但是,老實說,我還沒有得到它。 任何輸入將不勝感激。

回答

5

您將使用具有前景和背景視圖以及UIPanGestureRecognizer的UITableViewCell子類。此識別器將觸發滑動並處理前景視圖的移動。

這麼說,你會發現這裏的實現:https://github.com/spilliams/sparrowlike

重要的位:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [panGestureRecognizer setDelegate:self]; 
    [cell addGestureRecognizer:panGestureRecognizer]; 

    return cell; 
} 

#pragma mark - Gesture recognizer delegate 
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer 
{ 
    CustomCell *cell = (CustomCell *)[panGestureRecognizer view]; 
    CGPoint translation = [panGestureRecognizer translationInView:[cell superview] ]; 
    return (fabs(translation.x)/fabs(translation.y) > 1) ? YES : NO; 
} 

#pragma mark - Gesture handlers 

-(void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer 
{ 
    float threshold = (PAN_OPEN_X+PAN_CLOSED_X)/2.0; 
    float vX = 0.0; 
    float compare; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell *)[panGestureRecognizer view] ]; 
    UIView *view = ((CustomCell *)panGestureRecognizer.view).frontView; 

    switch ([panGestureRecognizer state]) { 
     case UIGestureRecognizerStateBegan: 
      if (self.openCellIndexPath.section != indexPath.section || self.openCellIndexPath.row != indexPath.row) { 
       [self snapView:((CustomCell *)[self.tableView cellForRowAtIndexPath:self.openCellIndexPath]).frontView toX:PAN_CLOSED_X animated:YES]; 
       [self setOpenCellIndexPath:nil]; 
       [self setOpenCellLastTX:0]; 
      } 
      break; 
     case UIGestureRecognizerStateEnded: 
      vX = (FAST_ANIMATION_DURATION/2.0)*[panGestureRecognizer velocityInView:self.view].x; 
      compare = view.transform.tx + vX; 
      if (compare > threshold) { 
       [self snapView:view toX:PAN_CLOSED_X animated:YES]; 
       [self setOpenCellIndexPath:nil]; 
       [self setOpenCellLastTX:0]; 
      } else { 
       [self snapView:view toX:PAN_OPEN_X animated:YES]; 
       [self setOpenCellIndexPath:[self.tableView indexPathForCell:(CustomCell *)panGestureRecognizer.view] ]; 
       [self setOpenCellLastTX:view.transform.tx]; 
      } 
      break; 
     case UIGestureRecognizerStateChanged: 
      compare = self.openCellLastTX+[panGestureRecognizer translationInView:self.view].x; 
      if (compare > PAN_CLOSED_X) 
       compare = PAN_CLOSED_X; 
      else if (compare < PAN_OPEN_X) 
       compare = PAN_OPEN_X; 
      [view setTransform:CGAffineTransformMakeTranslation(compare, 0)]; 
      break; 
     default: 
      break; 
    } 
} 
-(void)snapView:(UIView *)view toX:(float)x animated:(BOOL)animated 
{ 
    if (animated) { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:FAST_ANIMATION_DURATION]; 
    } 

    [view setTransform:CGAffineTransformMakeTranslation(x, 0)]; 

    if (animated) { 
     [UIView commitAnimations]; 
    } 
} 
+0

這似乎很容易實現。明天早上我會試一試。謝謝。 – Aleph72

+0

我想你可以添加一些不必要的位操作符和指針雜耍,使它更復雜;) – vikingosegundo

+0

好的。這種效果對於我正在嘗試做的事情來說是完美的。非常感謝vikingosegundo。 – Aleph72

相關問題