自定义Cell,使其左滑显示删除和分享

自定义JyyCell

JyyCell.h 文件
#import <UIKit/UIKit.h>

@class JyyCell;

@protocol JyyCellDelegate <NSObject>

-(void)judgeCellIsCloseDelegate:(JyyCell *)cell;
-(void)shareButtonClick;
-(void)deleteButtonClick;

@end

@interface JyyCell : UITableViewCell<UIGestureRecognizerDelegate>
{
    CGFloat width;
    CGFloat height;
}
@property (nonatomic, assign)id<JyyCellDelegate>delegate;

@property (nonatomic, retain)UIView *containerView;
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)UIButton *leftButton;
@property (nonatomic, retain)UIButton *rightButton;
@property (nonatomic, retain)UIButton *numberButton;
@property (nonatomic, assign)CGPoint panStartPoint;

@end

JyyCell.m 文件

初始化视图
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

-(void)commonInit
{
    width = ([UIScreen mainScreen].bounds.size.width);
    height = 63;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    //分享按钮
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = CGRectMake(width-height*2, -1, height, height+1);
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"icon_share_normal"] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_leftButton];
    
    //删除按钮
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(width-height-1, -1, height, height+1);
    [_rightButton setBackgroundImage:[UIImage imageNamed:@"icon_delete_normal"] forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_rightButton];
    
    //盖在按钮上的view
    _containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height-1)];
    _containerView.userInteractionEnabled = YES;
    _containerView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_containerView];
    
    //在containerView上加拖拽手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panCell:)];
    pan.delegate = self;
    [_containerView addGestureRecognizer:pan];
    
    //显示在cell上的内容
    _label = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, width-45-20, height)];
    _label.numberOfLines = 1;
    [_containerView addSubview:_label];
    
    //分割线,修饰作用,可不加
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(20, height-1, width-20, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:lineView];
}

分享按钮的方法
/**
 *  点击左边按钮分享的方法(具体实现用协议来实现)
 *
 *  @param button
 */
- (void)leftButtonAction:(UIButton *)button
{
    if ([_delegate respondsToSelector:@selector(shareButtonClick)]) {
        [_delegate shareButtonClick];
    }
}

删除按钮的方法
/**
 *  点击右边按钮删除的方法(具体实现用协议来实现)
 *
 *  @param button
 */
- (void)rightButtonAction:(UIButton *)button
{
    if ([_delegate respondsToSelector:@selector(deleteButtonClick)]) {
        [_delegate deleteButtonClick];
    }
}

拖拽手势的方法
/**
 *  拖拽方法
 *
 *  @param panGesture
 */
-(void)panCell:(UIPanGestureRecognizer *)panGesture
{
    CGFloat setX = 63;
    CGFloat distance = 2*setX;
    static CGFloat startX,durationX,containerX;
    //相对于屏幕上触摸点的坐标
    CGPoint touchPoint = [panGesture locationInView:[[UIApplication sharedApplication] keyWindow]];
    
    //以下两行代码是判断拖拽手势是偏于上下滑还是偏于左右滑,如果是左右滑才会执行拖拽的手势方法,如果是上下滑不会执行拖拽的手势方法
    CGPoint currentPoint = [panGesture translationInView:self.containerView];
    BOOL movingHorizontally = fabs(self.panStartPoint.y) < fabs(self.panStartPoint.x);
    
    
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        self.panStartPoint = currentPoint;
        startX = touchPoint.x;
        containerX = _containerView.frame.origin.x;
        //拖拽前把其他的cell回归到原位(下面有具体执行方法的代码)
        if ([_delegate respondsToSelector:@selector(judgeCellIsCloseDelegate:)]) {
            [_delegate judgeCellIsCloseDelegate:self];
        }
    }
    
    //当movingHorizontally==YES 代表是左右滑,执行拖拽的方法
    if (panGesture.state == UIGestureRecognizerStateChanged && movingHorizontally)
    {
        CGFloat currentX = touchPoint.x;
        durationX = currentX - startX;
        
        if (containerX == 0) {
            _containerView.frame = CGRectMake(containerX+durationX, 0, width, height-1);
            if (durationX < 0 && durationX < -distance) {
                _containerView.frame = CGRectMake(-distance, 0, width, height-1);
            } else if (durationX >= 0) {
                _containerView.frame = CGRectMake(0, 0, width, height-1);
            }
        } else if (containerX == -distance) {
            _containerView.frame = CGRectMake(containerX+durationX, 0, width, height);
            if (durationX > 0 && durationX > distance) {
                _containerView.frame =CGRectMake(0, 0, width, height);
            } else if (durationX <= 0) {
                _containerView.frame = CGRectMake(-distance, 0, width, height);
            }
        }
    }
    if (panGesture.state == UIGestureRecognizerStateEnded && movingHorizontally) {
        
        CGFloat currentX = touchPoint.x;
        durationX = currentX - startX;
        
        if (containerX == 0) {
            if (durationX < 0) {
                if (durationX > -height) {
                    //回到原处
                    [UIView animateWithDuration:0.2 animations:^{
                        _containerView.frame = CGRectMake(0, 0, width, height-1);
                    } completion:^(BOOL finished) {
                        _containerView.frame = CGRectMake(0, 0, width, height-1);
                    }];
                } else if (durationX <= -height) {
                    [UIView animateWithDuration:0.2 animations:^{
                        _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                    } completion:^(BOOL finished) {
                        _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                    }];
                }
            } else {
                //回到原处
                [UIView animateWithDuration:0.2 animations:^{
                    _containerView.frame = CGRectMake(0, 0, width, height-1);
                } completion:^(BOOL finished) {
                    _containerView.frame = CGRectMake(0, 0, width, height-1);
                }];
            }
        } else if (containerX == -distance) {
            if (durationX > 0) {
                if (durationX < height) {
                    [UIView animateWithDuration:0.2 animations:^{
                        _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                    } completion:^(BOOL finished) {
                        _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                    }];
                } else if (durationX >= height) {
                    [UIView animateWithDuration:0.2 animations:^{
                        _containerView.frame = CGRectMake(0, 0, width, height-1);
                    } completion:^(BOOL finished) {
                        _containerView.frame = CGRectMake(0, 0, width, height-1);
                    }];
                }
            } else {
                [UIView animateWithDuration:0.2 animations:^{
                    _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                } completion:^(BOOL finished) {
                    _containerView.frame = CGRectMake(-height*2, 0, width, height-1);
                }];
            }
        }
    }
}

UIGestureRecognizerDelegate协议方法

/**
 *  实现手势协议(必须实现此协议,否则tableview不能滑动)
 *
 *  @param gestureRecognizer
 *  @param otherGestureRecognizer 
 *
 *  @return yes可以识别滑动手势,no不识别滑动手势
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer*)gestureRecognizer;
        CGPoint velocity = [panGesture velocityInView:self.containerView];
        if (velocity.x > 0) {
            return YES;
        } else if (fabs(velocity.x) > fabs(velocity.y)) {
            return NO;
        }
    }
    return YES;
}

JyyCellDelegate的协议方法(滑动开始前,其他cell回归到原位)

-(void)judgeCellIsCloseDelegate:(JyyCell *)cell
{
    NSArray *arr = [self.tableView visibleCells];
    NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:arr];
    for (JyyCell *jyycell in mutableArr) {
        if (![jyycell isEqual:cell]) {
            if (jyycell.containerView.frame.origin.x != 0) {
                [UIView animateWithDuration:0.2 animations:^{
                    jyycell.containerView.frame = CGRectMake(0, 0, WIDTH, 63);
                } completion:^(BOOL finished) {
                    jyycell.containerView.frame = CGRectMake(0, 0, WIDTH, 63);
                }];
            }
        }
    }

}


截图类似于




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值