TableViewCell自定义左滑手势
2016-07-15 本文已影响482人
忘仙
1.首先看看系统的左滑的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//此处写要删除的内容
CollectionGoodsModel *goodsModel = _collectionListSource[indexPath.row];
NSString *collected_id = goodsModel.collect_id;
__weak typeof(self) wSelf = self;
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
view.backgroundColor=[UIColor clearColor];
[self.view addSubview:view];
// [LBProgressHUD showHUDto:view text:@"正在加载..."];
[FFService deleteCollectGoods:collected_id view:nil callback:^(id obj,NSError* error) {
ShoppingCarCommon *shopCommon = obj;
[LBProgressHUD hideAllHUDs];
if (shopCommon.success) {
//删除哪一行
[wSelf.collectionListSource removeObjectAtIndex:indexPath.row];
[wSelf.myTableView reloadData];
[view removeFromSuperview];
}
}];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"取消收藏";//向左滑动显示的文字
}
2.自定义左滑手势
废话少说,直接看代码
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipe;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe;
@property (nonatomic, strong) UILabel *swipeLabel;
@property (nonatomic, strong) UILabel *deleteLabel;
-(void)initViews{
_deleteLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, 100, self.bounds.size.height)];
_deleteLabel.text = @"删除";
_deleteLabel.textAlignment = NSTextAlignmentCenter;
_deleteLabel.backgroundColor = [UIColor greenColor];
[self addSubview:_deleteLabel];
//然后向_deleteLabel添加事件或者代理方法,在事件中执行删除或者其他操作
_swipeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.bounds.size.height)];
_swipeLabel.text = @"咬定青山不放松";
[self addSubview:_swipeLabel];
self.leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:self.leftSwipe];
[self addGestureRecognizer:self.rightSwipe];
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//改变位置
_swipeLabel.transform = CGAffineTransformMakeTranslation(-100, 0);
_deleteLabel.transform = CGAffineTransformMakeTranslation(-100, 0);
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
//还原
_swipeLabel.transform = CGAffineTransformIdentity;
_deleteLabel.transform = CGAffineTransformIdentity;
}
}
这是最简单的方法,当然后面还有一些逻辑判断,如有需要,请道友自行解决