“AnyDo”滑动以删除动画

时间:2013-02-22 10:46:21

标签: ios xcode animation

Any do swipe to delete animation

我正在尝试在滑动中实现表格视图单元格删除,就像它在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.
       } 

}

2 个答案:

答案 0 :(得分: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];
}

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

答案 1 :(得分:3)

Colin Eberhardt写了一篇关于How to Make a Gesture-Driven To-Do List App Like Clear的精彩教程。还实现了滑动以删除功能。