自定义删除
来源:互联网 发布:步步高v206b软件现在 编辑:程序博客网 时间:2024/05/16 05:58
#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
1 0
- 自定义删除
- GridView自定义编辑、删除
- GridView自定义编辑、删除
- 删除自定义的名称
- 删除空格自定义函数
- 批量删除自定义栏目
- js 自定义删除值
- 【javascript】数组自定义删除
- UITableViewCell自定义删除
- 滑动删除--自定义ListView
- 删除用户自定义配置
- Android_自定义删除View
- 自定义控件--滑动删除
- 自定义的删除edittext
- 自定义删除Cell
- 删除dedecms自定义字段
- tableViewCell删除按钮自定义
- 自定义删除单元格
- 通过DYLD_INSERT_LIBRARIES实现基于动态库的hook功能
- iOS广告轮播视图
- 安利App介绍及下载(含二维码)
- LeetCode:Search a 2D Matrix II
- 详解ListView加载网络图片的优化
- 自定义删除
- LFS 中文版手册发布:如何打造自己的 Linux 发行版
- 黑马程序员——递归与枚举
- iOS创建线程方式(以及回到主线程, 以及延时之行代码)
- 生活随笔:喜欢上surface book
- vps + Shadowsocks
- ACM数论一些简单结论和编程小技巧总结
- 现代操作系统——多处理机系统
- 绑定本地Service并与之通信