您的位置:首页 > 产品设计 > UI/UE

iOS开发给UITableView的单元格做一个类似于QQ和微信的侧滑露出删除按钮的思路

2017-05-15 15:58 447 查看
可以在tableView的cell的contentView的的下方添加删除和编辑按钮,然后给cell的contentView添加平移手势,滑动时候让cell的contentView左滑从而露出后面的删除和别的按钮

具体代码:

1.awakeFromNib方法中添加按钮和手势

- (void)awakeFromNib {
    [super
awakeFromNib];
    // Initialization code
    
    _deleteBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-70,
0, 70,
60)];
    [_deleteBtn
setTitle:@"删除"
forState:UIControlStateNormal];
    _deleteBtn.backgroundColor = [UIColor
redColor];
    [_deleteBtn
addTarget:self
action:@selector(deleteBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
    [self
insertSubview:_deleteBtn
belowSubview:self.contentView];
    
    _editBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-140,
0, 70,
60)];
    [_editBtn
setTitle:@"编辑"
forState:UIControlStateNormal];
    _editBtn.backgroundColor = [UIColor
grayColor];
    [_editBtn
4000
addTarget:self
action:@selector(editBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
    [self
insertSubview:_editBtn
belowSubview:self.contentView];
    
  
    //.平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panAct:)];
    pan.delegate =
self;

    pan.delaysTouchesBegan =
YES;

    //添加平移手势
    [self.contentView
addGestureRecognizer:pan];
}

平移方法

//平移方法
- (void)panAct:(UIPanGestureRecognizer *)pan{
    
    CGPoint point = [pan
translationInView:pan.view];

    CGFloat value = point.x;
    if (self.contentView.left < -(70
+ 70) * 1.1 ||
self.contentView.left >
30) {
        value = 0;
    }
    self.contentView.left += value;

    if (pan.state ==
UIGestureRecognizerStateEnded) {
        CGFloat x =
0;
        if (self.contentView.left
< -30 && !self.isSlided) {
            x = -140;
            self.isSlided =
YES;
        }else{
            self.isSlided =
NO;
        }
        [UIView
animateWithDuration:0.4
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:2
options:UIViewAnimationOptionLayoutSubviews
animations:^{
            self.contentView.left = x;
        } completion:^(BOOL finished) {
        }];
        
    }
    
    [pan setTranslation:CGPointZero
inView:pan.view];
    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐