#import <UIKit/UIKit.h>
@class Cell;
typedef NS_ENUM(NSInteger, ActionButtonDirection)
{
ActionButtonDirectionLeft,
ActionButtonDirectionRight
};
//按钮点击回调
typedef void(^ActionButtonHandle)(Cell *c);
//开始拖动回调
typedef void(^BeginPanGestureHandle)(Cell *c);
@interface Cell : UITableViewCell
//名字
@property (nonatomic, copy) NSString *name;
//设置按钮标题
- (void)setActionButtonWithTitle:(NSString *)title direction:(ActionButtonDirection)direction handle:(ActionButtonHandle)handle;
//设置开始拖动的回调
- (void)setBeginSwipeGestureHandle:(BeginPanGestureHandle)handle;
@end
#import "Cell.h"
//按钮个数
#define kNumberOfActionButton 2
//按钮高度
#define kActionButtonHeight self.contentView.frame.size.height
//按钮宽度
#define kActionButtonWidth kActionButtonHeight
//右边按钮的tag
#define kActionRightButtonTag 1
//左边按钮的tag
#define kActionLeftButtonTag 2
@interface Cell ()<UIGestureRecognizerDelegate>
{
//左边按钮回调
ActionButtonHandle _leftHandle;
//右边按钮回调
ActionButtonHandle _rightHandle;
//开始拖动的回调
BeginPanGestureHandle _beginPanHandle;
}
//内容视图
@property (nonatomic, weak) UIView *cellContentView;
@property (nonatomic, weak) UILabel *nameLabel;
@end
@implementation Cell
#pragma mark - 初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self initialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self initialize];
}
return self;
}
- (void)initialize
{
//1.创建按钮
[self createActionButton];
//2.添加手势
[self addPanGestureToCellContentView];
}
#pragma mark - 懒加载创建视图
- (UIView *)cellContentView
{
if (!_cellContentView)
{
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor orangeColor];
[self.contentView addSubview:v];
_cellContentView = v;
}
return _cellContentView;
}
- (UILabel *)nameLabel
{
if (!_nameLabel)
{
UILabel *l = [[UILabel alloc] init];
[self.cellContentView addSubview:l];
_nameLabel = l;
}
return _nameLabel;
}
#pragma mark - setter方法
- (void)setName:(NSString *)name
{
_name = name;
self.nameLabel.text = _name;
}
//设置按钮标题
- (void)setActionButtonWithTitle:(NSString *)title direction:(ActionButtonDirection)direction handle:(ActionButtonHandle)handle
{
//1.按钮赋值
NSInteger tag = kActionLeftButtonTag;
if (direction == ActionButtonDirectionRight)
{
tag = kActionRightButtonTag;
_rightHandle = handle;
}
else
{
_leftHandle = handle;
}
UIButton *btn = (UIButton *)[self.contentView viewWithTag:tag];
[btn setTitle:title forState:UIControlStateNormal];
}
//设置开始拖动的回调
- (void)setBeginSwipeGestureHandle:(BeginPanGestureHandle)handle
{
_beginPanHandle = handle;
}
#pragma mark - 创建按钮
- (void)createActionButton
{
for (int i = 0; i < kNumberOfActionButton; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIColor *backgroundColor;
if (i == 0)
{
button.tag = kActionRightButtonTag;
backgroundColor = [UIColor lightGrayColor];
}
else
{
button.tag = kActionLeftButtonTag;
backgroundColor = [UIColor purpleColor];
}
button.backgroundColor = backgroundColor;
[button addTarget:self action:@selector(buttonHandle:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
}
}
#pragma mark - 按钮点击处理事件
- (void)buttonHandle:(UIButton *)button
{
if (button.tag == kActionLeftButtonTag && _leftHandle)
{
_leftHandle(self);
}
else if(button.tag == kActionRightButtonTag && _rightHandle)
{
_rightHandle(self);
}
}
#pragma mark - layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];
self.cellContentView.frame = self.contentView.bounds;
self.nameLabel.frame = self.contentView.bounds;
//设置按钮坐标大小
for (int i = 0; i < kNumberOfActionButton; i++)
{
UIButton *btn = (UIButton *)[self.contentView viewWithTag:i+1];
btn.frame = CGRectMake(self.frame.size.width - (i+1)*kActionButtonWidth, 0, kActionButtonWidth, kActionButtonHeight);
}
}
/**
* 添加拖动手势
*/
- (void)addPanGestureToCellContentView
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandle:)];
panGesture.delegate = self;
[self.cellContentView addGestureRecognizer:panGesture];
}
#pragma mark - 手势处理方法
/**
* 手势处理方法
*
* @param panGesture <#panGesture description#>
*/
- (void)panHandle:(UIPanGestureRecognizer *)panGesture
{
//手势开始
if (panGesture.state == UIGestureRecognizerStateBegan)
{
if (_beginPanHandle)
{
_beginPanHandle(self);
}
}
//手势移动
else if(panGesture.state == UIGestureRecognizerStateChanged)
{
//1.获取移动的坐标
CGPoint point = [panGesture translationInView:self.contentView];
//中心点x坐标
CGFloat centerX = self.cellContentView.center.x + point.x;
//设置边界 self.center.x - 2*kActionButtonWidth <= centerX <= self.contentView.center.x
if (centerX >= self.contentView.center.x)
{
centerX = self.contentView.center.x;
}
else if (centerX <= self.center.x - 2*kActionButtonWidth)
{
centerX = self.center.x - 2*kActionButtonWidth;
}
//2.修改中心坐标
self.cellContentView.center = CGPointMake(centerX, self.cellContentView.center.y);
//3.清空累加值
[panGesture setTranslation:CGPointZero inView:self.contentView];
}
else if(panGesture.state == UIGestureRecognizerStateEnded)
{
NSLog(@"手势结束");
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma UIGestureDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint point = [pan translationInView:self.contentView];
//左右滑动
if (fabs(point.x) > fabs(point.y))
{
return YES;
}
}
return NO;
}
@end