1

任何滑动以删除动画

我正在尝试在滑动时实现表格视图单元格删除,就像在 AnyDo 应用程序中一样。有什么帮助???

我的代码:

我正在使用手势识别器

      - (UISwipeGestureRecognizer *)swipeRightRecognizer 
      { 
        if (!_swipeRightRecognizer)  
           {
            _swipeRightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
            _swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
       }  

        return _swipeRightRecognizer; 
       }

向单元格添加手势:

       [cell.contentView addGestureRecognizer:self.leftGestureRecognizer];

处理滑动。

       - (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer indexPath: (NSIndexPath *)index
       { 
       if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
       { 
        //Table View deletion code.  + Line to be drawn on cell, move that cell to the bottom of the table with animaiton.
       } 

}

4

2 回答 2

3

您可以使用UISwipeGestureRecognizer

UISwipeGestureRecognizer *gRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(yourSelector:)];
[gRec setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];

[yourCell addGestureRecognizer:gRec];

在 yourSelector 方法中,您可以处理滑动:

- (void)yourSelector:(id)sender {
    NSLog(@"Handle swipe");
    UISwipeGestureRecognizer *gRec = sender;
    UITableViewCell *cell = [gRec view];

    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,0,0)];
    [line setImage:[UIImage imageNamed:@"line.png"]];
    [UIView animateWithDuration:0.3 animation:^ (void) {
        [cell addSubview:line];
        [line setFrame:CGRectMake(x,y,320,5];
    } completion:nil];
}

编辑:我添加了该行的代码,但我现在无法尝试。但是,它应该工作

于 2013-02-22T10:50:06.923 回答
3

Colin Eberhardt 写了一篇很棒的教程,介绍如何制作像 Clear 一样的手势驱动的待办事项列表应用程序。还实现了滑动删除功能。

于 2013-02-22T10:58:48.313 回答