2012-04-30 13 views
3

データベースからデータを取り込むセルを含む動的なテーブルビューがあります。 セルが選択されている場合、ユーザーは他にもいくつかのオプションを選択する必要があります。 セルが選択されたときに別のビューをプッシュする方法はわかっていますが、この方法はグラフィカルには好きではありません。 たとえば、同じセルを反転してオプションを表示した後で、スワイプで戻すことができます。 または、セル全体が画面から外れてオプションが表示されたり、別のビューがセルからスライドして元に戻ったりする可能性があります。選択したときにテーブルセルにスライドビューを追加する

どのソリューションが最も簡単なのですか? 誰かが私を正しい方向に向けることができますか?私はもちろんコードは必要ありません、私はここで学ぶために、私はちょうど見るべきものを知る必要があります。 これまでは、UITableViewCellのサブクラス化に関する記事を読んだことがありますが、正直なところ、私はまだそれを持っていません。 すべての入力をいただければ幸いです。

答えて

5

フォアグラウンドビューとバックグラウンドビューを持つUITableViewCellサブクラスと、UIPanGestureRecognizerを使用します。この認識装置はスワイプをトリガーし、フォアグラウンド・ビューの移動を処理します。 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

Ok。この効果は、私がやろうとしていたことに最適です。ヴィッキングスグンドありがとうございました。 – Aleph72

関連する問題