4

我有一个动态表视图,其中包含从数据库填充的单元格。当一个单元格被选中时,用户应该可以选择很少的其他选项。我知道在选择单元格时如何推送另一个视图,但我不喜欢这种图形化方法。例如,如果同一个单元格可以翻转并显示选项(然后翻转)可能会更好。或者整个单元格可以从屏幕上滑出以显示选项,或者另一个视图可以从单元格中向下滑动然后向上滑动。

这些解决方案中哪一个最容易做到?谁能指出我正确的方向?我当然不需要代码,我是来学习的,我只需要知道要看什么。到目前为止,我已经阅读了一些关于子类化 UITableViewCell 的内容,但是,老实说,我还没有得到它。任何输入将不胜感激。

4

1 回答 1

6

您将使用具有前景和背景视图以及 UIPanGestureRecognizer 的 UITableViewCell 子类。此识别器将触发滑动并处理前景视图的移动。

也就是说,你会在这里找到一个实现:https ://github.com/spiliams/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];
    }
}
于 2012-04-30T19:19:45.183 回答