#import "BaseTableViewCell.h"
@interface BaseTableViewCell (TabelViewCellSideslip)
/**
操作回调
@param flag -1:删除 0:关闭 1:打开 -2:将要关闭 2:将要打开
*/
typedef void (^TabelViewCellSideslipBlock)(BaseTableViewCell *cell,UIButton *btn,NSInteger flag);
/**
设置手势
@param block 操作回调
*/
- (void)addSideslipSwipeGesBlock:(TabelViewCellSideslipBlock)block;
/**
关闭与否
@param isClose 是否关闭
*/
- (void)cellSlipSwipeIsClose:(BOOL)isClose;
/**
是否打开手势效果 默认打开
@param isEnable 是否打开
*/
- (void)setCellSwipeHandleEnable:(BOOL)isEnable;
@end
//
// BaseTableViewCell+TabelViewCellSideslip.m
//
#import "BaseTableViewCell+TabelViewCellSideslip.h"
#import "Masonry.h"
#import "NSObject+BaseCommonFun.h"
@interface BaseTableViewCell()
@property (nonatomic, strong) UIButton *cacelBtn;
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipe;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe;
@property (nonatomic, copy) TabelViewCellSideslipBlock block;
@end
@implementation BaseTableViewCell (TabelViewCellSideslip)
- (void)setCacelBtn:(UIButton *)cacelBtn{
objc_setAssociatedObject(self, @selector(cacelBtn), cacelBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIButton *)cacelBtn{
UIButton *cacelBtn = objc_getAssociatedObject(self, @selector(cacelBtn));
return cacelBtn;
}
- (void)setBlock:(TabelViewCellSideslipBlock)block{
objc_setAssociatedObject(self, @selector(block), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (TabelViewCellSideslipBlock)block{
TabelViewCellSideslipBlock block = objc_getAssociatedObject(self, @selector(block));
return block;
}
- (void)setLeftSwipe:(UISwipeGestureRecognizer *)leftSwipe{
objc_setAssociatedObject(self, @selector(leftSwipe), leftSwipe, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)setRightSwipe:(UISwipeGestureRecognizer *)rightSwipe{
objc_setAssociatedObject(self, @selector(rightSwipe), rightSwipe, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UISwipeGestureRecognizer *)leftSwipe{
UISwipeGestureRecognizer *leftSwipe = objc_getAssociatedObject(self, @selector(leftSwipe));
return leftSwipe;
}
- (UISwipeGestureRecognizer *)rightSwipe{
UISwipeGestureRecognizer *rightSwipe = objc_getAssociatedObject(self, @selector(rightSwipe));
return rightSwipe;
}
- (void)addSideslipSwipeGesBlock:(TabelViewCellSideslipBlock)block{
[self creatCancelBtn];
[self creatSwipGes];
if (!self.block) {
self.block = block;
}
}
- (void)creatSwipGes{
if (!self.leftSwipe) {
self.leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipe.delegate = self;
[self.contentView addGestureRecognizer:self.leftSwipe];
self.leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
}
if (!self.rightSwipe) {
self.rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipe.delegate = self;
[self.contentView addGestureRecognizer:self.rightSwipe];
self.rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
}
}
- (void)creatCancelBtn{
if (!self.cacelBtn) {
self.cacelBtn = [self common_btnTitle:@"删除" color:TWColor_ffffff fontSize:15 target:self action:@selector(cancelBtnAction:)];
self.cacelBtn.backgroundColor = [UIColor redColor];
// [self addSubview:self.cacelBtn];
[self insertSubview:self.cacelBtn belowSubview:self.contentView];
self.cacelBtn.hidden = YES;
[self.cacelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.height.right.equalTo(self);
make.width.equalTo(@80);
}];
}
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
[self cellSlipSwipeIsClose:NO];
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
[self cellSlipSwipeIsClose:YES];
}
}
- (void)cellSlipSwipeIsClose:(BOOL)isClose{
if (isClose) {
if(self.block)self.block(self,nil,-2);//将要关闭
self.contentView.transform = CGAffineTransformIdentity;
self.cacelBtn.hidden = YES;
if(self.block)self.block(self,nil,0);
}else{
if(self.block)self.block(self,nil,2);//将要打开
self.contentView.transform = CGAffineTransformMakeTranslation(-80, 0);
self.cacelBtn.hidden = NO;
if(self.block)self.block(self,nil,1);
}
}
- (void)setCellSwipeHandleEnable:(BOOL)isEnable{
self.leftSwipe.enabled = self.rightSwipe.enabled = isEnable;
}
- (void)cancelBtnAction:(UIButton *)btn{
[self cellSlipSwipeIsClose:YES];
if (self.block) {
self.block(self,btn,-1);//删除
}
}
@end
网友评论