可以在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 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];
}