Ios 选中时向表格单元格添加滑动视图

Ios 选中时向表格单元格添加滑动视图,ios,uitableview,Ios,Uitableview,我有一个动态表视图,其中单元格从db填充。 当选择一个单元格时,用户应该可以选择几个其他选项。 我知道在选择单元格时如何推送另一个视图,但我不喜欢这种图形化的方法。 例如,如果同一个单元格可以翻转并显示选项(然后翻转回来),可能会更好。 或者整个单元格可以从显示选项的屏幕上滑下,或者另一个视图可以从单元格上滑下,然后再滑回 以下哪种解决方案最容易实现? 谁能给我指一下正确的方向吗?当然,我不需要代码,我在这里学习,我只需要知道看什么。 到目前为止,我已经阅读了一些关于UITableViewCel

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

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

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

也就是说,您可以在这里找到一个实现:

重要的是:

- (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];
    }
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“CustomCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
//配置单元格。。。
UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[panGestureRecognizer setDelegate:self];
[小区地址识别器:泛感知识别器];
返回单元;
}
#pragma标记-手势识别器委托
-(BOOL)手势识别器应开始:(UIPanGestureRecognizer*)panGestureRecognizer
{
CustomCell*单元格=(CustomCell*)[PangestureRecognitizer视图];
CGPoint translation=[PangestureRecognitor translationView:[cell superview]];
返回(fabs(translation.x)/fabs(translation.y)>1)?是:否;
}
#pragma标记-手势处理程序
-(无效)手持面板:(UIPangEstureRecognitor*)PangEstureRecognitor
{
浮动阈值=(平移打开X+平移关闭X)/2.0;
浮动vX=0.0;
浮动比较;
NSIndexPath*indexPath=[self.tableView indexPathForCell:(CustomCell*)[PangestureRecognitor视图]];
UIView*视图=((CustomCell*)panGestureRecognizer.view).frontView;
开关([PangestureRecognitor状态]){
案例UIgestureRecognitzerStateStart:
if(self.openCellIndexPath.section!=indexPath.section | | self.openCellIndexPath.row!=indexPath.row){
[self snapView:((CustomCell*)[self.tableView cellForRowAtIndexPath:self.openCellIndexPath])。frontView toX:PAN_CLOSED_X动画:是];
[self-setOpenCellIndexath:nil];
[self-setOpenCellLastTX:0];
}
打破
案例UIgestureRecognitzerStateEnded:
vX=(快速动画持续时间/2.0)*[PangestureRecognitor VelocityView:self.view].x;
比较=view.transform.tx+vX;
如果(比较>阈值){
[自snapView:view toX:PAN_CLOSED_X动画:是];
[self-setOpenCellIndexath:nil];
[self-setOpenCellLastTX:0];
}否则{
[自snapView:view toX:PAN_OPEN_X动画:是];
[self-setOpenCellIndexPath:[self.tableView indexPathForCell:(CustomCell*)PangestureRecognitizer.view];
[self-setOpenCellLastTX:view.transform.tx];
}
打破
案例UIgestureRecognitzerStateChanged:
compare=self.opencelllastx+[PangestureRecognitor TranslationView:self.view].x;
如果(比较>平移关闭)
比较=PAN\u CLOSED\u X;
否则如果(比较