自定义UITableViewCell实现左滑动多菜单功能LeftSwipe
今天愚人节,小伙们,愚人节快乐!
实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图:

这里尝试用了下使用三个方式来实现了这个功能:
1、使用自定义UITableViewCell + UISwipeGestureRecognizer + 代理 实现;
2、使用自定义UITableViewCell + UIPanGestureRecognizer + 代理 实现;
3、使用自定义UITableViewCell + UISwipeGestureRecognizer + block 实现。
注意点: 使用UIPanGestureRecognizer手势实现左滑的时候,由于拖拽手势的方向随意性,导致与UITableViewController的下拉刷新手势冲突了!
感觉还是用UISwipeGestureRecognizer清扫手势实现好点!
部分代码:
1、使用UISwipeGestureRecognizer + Delegate
自定义UITableViewCell部分代码:
//
// TanTableViewCell.h
// Tan_SwipeTableViewCell
//
// Created by PX_Mac on 16/3/25.
// Copyright © 2016年 PX_Mac. All rights reserved.
// #import <UIKit/UIKit.h>
@class MemberModel;
@class TanTableViewCell; @protocol TanTableViewCellDelegate <NSObject> @optional
- (void)deleteMember: (TanTableViewCell *)cell; //协议方法:删除会员
- (void)closeOtherCellLeftSwipe; //关闭其他单元格的左滑 @end @interface TanTableViewCell : UITableViewCell //静态构造方法
+ (instancetype)cellWithTableView: (UITableView *)tableView; @property (nonatomic, strong) MemberModel *model; //模型属性
@property (nonatomic, weak) id<TanTableViewCellDelegate> delegate; //代理 - (void)setData: (MemberModel *)model; //设置要显示的数据
- (void)closeSwipe; //关闭滑动,恢复原样(用于在滑动当前单元格时,把其他已经左滑的单元格关闭) @end
@implementation TanTableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *reuseIdentity = @"tanCell";
TanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentity];
if (cell == nil){
cell = [[TanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentity];
}
return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
[self initSubControls]; //初始化子控件
}
return self;
}
//初始化子控件
- (void)initSubControls{
/*....... */
//3、给容器containerView绑定左右滑动清扫手势
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; //设置向左清扫
[self.containerView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;//设置向右清扫
[self.containerView addGestureRecognizer:rightSwipe];
self.selectionStyle = UITableViewCellSelectionStyleNone; //设置单元格选中样式
[self.contentView bringSubviewToFront:self.containerView]; //设置containerView显示在最上层
}
//左滑动和右滑动手势
- (void)swipe: (UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft){
if (self.isOpenLeft) return; //已经打开左滑,不再执行
//开始左滑: 先调用代理关闭其他cell的左滑
if ([self.delegate respondsToSelector:@selector(closeOtherCellLeftSwipe)])
[self.delegate closeOtherCellLeftSwipe];
[UIView animateWithDuration:0.5 animations:^{
sender.view.center = CGPointMake(, CELLHEIGHT * 0.5);
}];
self.isOpenLeft = YES;
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight){
[self closeSwipe]; //关闭左滑
}
}
//关闭左滑,恢复原状
- (void)closeSwipe{
if (!self.isOpenLeft) return; //还未打开左滑,不需要执行右滑
[UIView animateWithDuration:0.5 animations:^{
self.containerView.center = CGPointMake(SCREENWIDTH * 0.5, CELLHEIGHT * 0.5);
}];
self.isOpenLeft = NO;
}
//.....
@end
控制器部分代码:
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TanTableViewCell *cell = [TanTableViewCell cellWithTableView:tableView];
cell.delegate = self; MemberModel *model = [self.dataArr objectAtIndex:indexPath.row];
[cell setData:model]; return cell;
} #pragma mark - cell代理方法
//删除单元格
- (void)deleteMember:(TanTableViewCell *)cell{
NSIndexPath *path = [self.tableView indexPathForCell:cell]; //获取cell所在位置
//删除数组中数据
[self.dataArr removeObjectAtIndex:path.row];
//删除单元格
[self.tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
} //关闭其他cell的左滑
- (void)closeOtherCellLeftSwipe{
//循环显示的cell
for (TanTableViewCell *item in self.tableView.visibleCells) {
[item closeSwipe];
}
}
2、UIPanGestureRecognizer + 代理
自定义UITableViewCell部分代码:
//初始化子控件
- (void)initSubControls{
/* ...... */ //3、给容器containerView绑定拖动手势
UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.containerView addGestureRecognizer:panGes];
self.panGes = panGes; self.selectionStyle = UITableViewCellSelectionStyleNone; //设置单元格选中样式
[self.contentView bringSubviewToFront:self.containerView]; //设置containerView显示在最上层
} //拖动手势(拖拽手势和UITableView的下拉刷新手势有冲突,造成下拉刷新不能使用)
- (void)pan: (UIPanGestureRecognizer *)sender
{
//动画结束时修正位置
if (sender.state == UIGestureRecognizerStateEnded){ //关闭其他cell的左拖拽
if ([self.delegate respondsToSelector:@selector(closeOtherCellLeftPan:)])
[self.delegate closeOtherCellLeftPan: self]; if (sender.view.frame.origin.x < -SCREENWIDTH * 0.25){
sender.view.transform = CGAffineTransformMakeTranslation(-SCREENWIDTH * 0.5, );
[sender setTranslation:CGPointZero inView:sender.view]; //必须归0
}
else{
[self closeLeftPan];
}
} CGPoint point = [sender translationInView:self.contentView]; CGFloat tx = sender.view.transform.tx; if (tx < - SCREENWIDTH * 0.5 || tx > ) return; //形变
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, );
[sender setTranslation:CGPointZero inView:sender.view]; //必须归0
} //关闭左拖拽
- (void)closeLeftPan{
self.panGes.view.transform = CGAffineTransformMakeTranslation(, );
[self.panGes setTranslation:CGPointZero inView:self.panGes.view]; //必须归0
}
3、UISwipeGestureRecognizer + block
自定义UITableViewCell部分代码:
//
// TanTableViewCell.h
// Tan_SwipeTableViewCell
//
// Created by PX_Mac on 16/3/25.
// Copyright © 2016年 PX_Mac. All rights reserved.
// #import <UIKit/UIKit.h>
@class MemberModel; @interface TanTableViewCell : UITableViewCell //静态构造方法
+ (instancetype)cellWithTableView: (UITableView *)tableView; @property (nonatomic, strong) MemberModel *model; //模型属性
- (void)setData: (MemberModel *)model; //设置要显示的数据 @property (nonatomic, copy) void (^deleteMember)(); //删除会员block回调方法
@property (nonatomic, copy) void (^closeOtherCellSwipe)(); //关闭其他cell的左滑 - (void)closeLeftSwipe; //关闭左滑 @end
//左滑动和右滑动手势
- (void)swipe: (UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft){
if (self.isOpenLeft) return; //已经打开左滑,不再执行 //开始左滑: 先调用block关闭其他可能左滑的cell
if (self.closeOtherCellSwipe)
self.closeOtherCellSwipe(); [UIView animateWithDuration:0.5 animations:^{
sender.view.center = CGPointMake(, CELLHEIGHT * 0.5);
}];
self.isOpenLeft = YES;
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight){
[self closeLeftSwipe]; //关闭左滑
}
} //关闭左滑,恢复原状
- (void)closeLeftSwipe{
if (!self.isOpenLeft) return; //还未打开左滑,不需要执行右滑 [UIView animateWithDuration:0.5 animations:^{
self.containerView.center = CGPointMake(SCREENWIDTH * 0.5, CELLHEIGHT * 0.5);
}];
self.isOpenLeft = NO;
}
控制器部分代码:
#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TanTableViewCell *cell = [TanTableViewCell cellWithTableView:tableView]; MemberModel *model = [self.dataArr objectAtIndex:indexPath.row];
[cell setData:model]; //设置数据 __weak typeof(self) tempSelf = self;
__weak typeof(cell) tempCell = cell; //设置删除cell回调block
cell.deleteMember = ^{
NSIndexPath *tempIndex = [tempSelf.tablView indexPathForCell:tempCell];
[tempSelf.dataArr removeObject:tempCell.model];
[tempSelf.tablView deleteRowsAtIndexPaths:@[tempIndex] withRowAnimation:UITableViewRowAnimationLeft];
}; //设置当cell左滑时,关闭其他cell的左滑
cell.closeOtherCellSwipe = ^{
for (TanTableViewCell *item in tempSelf.tablView.visibleCells) {
if (item != tempCell) [item closeLeftSwipe];
}
}; return cell;
}
DEMO下载:
github地址:https://github.com/xiaotanit/Tan_UITableViewCellLeftSwipe
csdn地址:http://download.csdn.net/detail/tandaxia/9479428
原文链接:http://www.cnblogs.com/tandaxia/p/5346659.html
自定义UITableViewCell实现左滑动多菜单功能LeftSwipe的更多相关文章
- 自定义uitableviewcell通过加上滑动手势进行删除对应的行。PS:用代理来实现
#import <UIKit/UIKit.h> @class ZSDCustomCell; //协议 @protocol ZSDCustomCellDelegate <NSObjec ...
- IOS uitableviewcell 向左滑动删除编辑等
主要实现这个方法就好了 -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActions ...
- Android自定义组件——四个方向滑动的菜单实现
今天无意中实现了一个四个方向滑动的菜单,感觉挺好玩,滑动起来很顺手,既然已经做出来了就贴出来让大家也玩弄一下. 一.效果演示 (说明:目前没有安装Android模拟器,制作的动态图片太卡了,就贴一下静 ...
- Android自定义组件系列【15】——四个方向滑动的菜单实现
今天无意中实现了一个四个方向滑动的菜单,感觉挺好玩,滑动起来很顺手,既然已经做出来了就贴出来让大家也玩弄一下. 一.效果演示 (说明:目前没有安装Android模拟器,制作的动态图片太卡了,就贴一下静 ...
- 使用Java语言开发微信公众平台(八)——自定义菜单功能
随着上一篇文章的结束,我们已经实现了所有消息的类型的回复功能.今天,我们来学习更加高大上,也更加重要的自定义菜单功能. 一.了解自定义菜单 自定义菜单是微信公众平台最常用也是最重要的功能之一.根据微信 ...
- 仿EXCEL插件,智表ZCELL产品V1.7 版本发布,增加自定义右键菜单功能
详细请移步 智表(ZCELL)官网www.zcell.net 更新说明 这次更新主要应用户要求,主要解决了自定义右键菜单事件的支持,并新增了公式中自定义函数传参.快捷键剪切等功能,欢迎大家体验使用. ...
- iOS之UITableView带滑动操作菜单的Cell
制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...
- iOS之UI--使用SWRevealViewController实现侧边菜单功能详解实例
使用SWRevealViewController实现侧边菜单功能详解 下面通过两种方法详解SWRevealViewController实现侧边菜单功能: 1.使用StoryBoard实现 2.纯代 ...
- iOS之UI--使用SWRevealViewController 实现侧边菜单功能详解实例
iOS之UI--使用SWRevealViewController 实现侧边菜单功能详解实例 使用SWRevealViewController实现侧边菜单功能详解 下面通过两种方法详解SWReveal ...
随机推荐
- SSD框架训练自己的数据集
SSD demo中详细介绍了如何在VOC数据集上使用SSD进行物体检测的训练和验证.本文介绍如何使用SSD实现对自己数据集的训练和验证过程,内容包括: 1 数据集的标注2 数据集的转换3 使用SSD如 ...
- ASP.NET Core Loves JavaScript
前言 在 ASP.NET 团队的 Github 的主页上,有这样一个开源项目叫:"JavaScriptsServices",那么 什么是 JavaScriptsServices 呢 ...
- ASP.Net MVC 5 in Xamarin Studio 5.2
Xamarin Studio 是一个Mono的跨平台 IDE(Integrated Development Environment),支持Wiindow和Mac,最新发布的5.2 版本支持ASP.NE ...
- 使用Metrics.NET 构建 ASP.NET MVC 应用程序的性能指标
通常我们需要监测ASP.NET MVC 或 Web API 的应用程序的性能时,通常采用的是自定义性能计数器,性能计数器会引发无休止的运维问题(损坏的计数器.权限问题等).这篇文章向你介绍一个新的替代 ...
- 续 CentOS7(mini) 运行MVC5 + Mariadb
上一篇,介绍了在CentOS7上使用mono官方二进制安装包快速安装mono环境 并且成功运行了一个Owin自宿主应用(Booker) 由于Owin自宿主应用不需要System.Web的支持,所以可以 ...
- 全球HTTPS时代已来,你跟上了吗?
全球HTTPS时代已来,你跟上了吗? 互联网发展20多年,大家都习惯了在浏览器地址里输入HTTP格式的网址.但前两年,HTTPS逐渐取代HTTP,成为传输协议界的"新宠". 早 ...
- Nova PhoneGap框架 总结
Nova PhoneGap Framework 是完全针对PhoneGap应用程序量身定做的,在这个框架下开发的应用程序很容易实现高质量的代码,很容易让程序拥有很好的性能和用户体验. 在经历了多个项目 ...
- 包含LOB_Data列的表删除大量数据后表及数据库文件的收缩
最近有一张表(内含varchar(max)字段),占用空间达到240G,删除历史数据后几十万条后,空间并未得到释放. 然后用DBCC CLEANTABLE(0,tb_name,100)来释放删除记录后 ...
- 关于apue.3e中apue.h的使用
关于apue.3e中apue.h的使用 近来要学一遍APUE第三版,并于此开博做为记录. 先下载源文件: # url: http://http//www.apuebook.com/code3e.htm ...
- PHP 调用webService方式
方法如下: <?php header('Content-Type: text/html; charset=UTF-8'); define('APP_ROOT', dirname(__FILE__ ...