iOS Swipe Tableviewcell(左右滑动出现按钮)
1.背景
看到QQ的左右滑动Tableviewcell时可以出现多个菜单,觉得很高大上,因为没这种需求,
也只是需要一个删除按钮,这个系统已经帮我们实现了,只需要实现几个代理就可以,做出左划
出现删除按钮,这个我就不再这里说了,Google上你应该很容易实现,这里要讲的是如何实现
多个按钮的左右滑动的实现。可以获取点击的是哪个按钮。
2.效果图

左右侧可以分开设置,即可以同时为图片,也可以一边图片一边文字。
3.实现原理
在cell里面按钮和手势,根据手势来控制视图的移动和显示。
4.自定义cell的核心代码
.h文件
#import <UIKit/UIKit.h>
#import "menuview.h" typedef enum {
CellStateLeft,
CellStateRight,
CellStateCenter, } CellState; @protocol MyTableViewCellDelegate <NSObject>
- (void) btnMenuClick:(NSInteger)index isRightMenu:(NSInteger) isright;
@end @interface myTableViewCell : UITableViewCell<UIGestureRecognizerDelegate,MenuviewDelegate>
{
CellState state;
}
@property (nonatomic,strong) id<MyTableViewCellDelegate> myTableViewCellDelegate; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
- (void) initLeftMenu:(NSArray *) btnLeft bagcolor:(NSArray *) leftCol width:(NSInteger)leftWid height:(NSInteger) righthig isText:(BOOL) flag;
- (void) initRightMenu:(NSArray *) btnRight bagcolor:(NSArray *) rightCol width:(NSInteger)rightWid height:(NSInteger) righthig isText:(BOOL) flag; @end
.m文件
//
// myTableViewCell.m
// Delecttable
//
// Created by apple on 14/12/12.
// Copyright (c) 2014年 Reasonable. All rights reserved.
// #import "myTableViewCell.h" @implementation myTableViewCell
{
NSArray * rightconn;
NSArray * rightcolor;
NSInteger rightwidth; BOOL rightIsText;
BOOL hasRightMenu; NSArray * leftconn;
NSArray * leftcolor;
NSInteger leftwidth;
BOOL leftIsText;
BOOL hasLeftMenu; menuview * leftview;
menuview * rightView; NSInteger height; }
- (void) initSubControl
{ } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
hasRightMenu=NO;
hasLeftMenu=NO;
state=CellStateCenter;
[self initControl];
} return self;
}
- (void) initRightMenu:(NSArray *) btnRight bagcolor:(NSArray *) rightCol width:(NSInteger)rightWid height:(NSInteger) righthig isText:(BOOL) flag
{
if ((btnRight.count> && btnRight.count==rightCol.count && flag) || ( btnRight.count> &&!flag)) {
rightconn=btnRight;
rightcolor=rightCol;
rightwidth=[self GetMenuWidth:rightWid isRight:YES];
rightIsText=flag;
state=CellStateCenter;
hasRightMenu=YES;
height=righthig;
[self initControl];
}
}
- (void) initLeftMenu:(NSArray *) btnLeft bagcolor:(NSArray *) leftCol width:(NSInteger)leftWid height:(NSInteger) righthig isText:(BOOL) flag
{
if ((btnLeft.count> && btnLeft.count==leftCol.count && flag) || ( btnLeft.count> &&!flag)) {
leftconn=btnLeft;
leftcolor=leftCol;
leftwidth=[self GetMenuWidth:leftWid isRight:YES];
leftIsText=flag;
state=CellStateCenter;
hasLeftMenu=YES;
height=righthig;
[self initControl];
}
} -(NSInteger) GetMenuWidth:(NSInteger)fucwidth isRight:(BOOL) flag
{
NSInteger totalwidth=flag?rightconn.count*rightwidth:leftconn.count * leftwidth;
NSInteger muennumber=flag?rightconn.count:leftconn.count;
if (totalwidth>self.frame.size.width) {
return (self.frame.size.width/muennumber);
}else{
return fucwidth;
} }
- (void) initControl
{
if (hasRightMenu) {
rightView=[[menuview alloc]initWithFrame:CGRectMake(self.frame.size.width-(rightwidth*rightconn.count), , rightwidth*rightconn.count,height)];
[rightView initMenu:rightconn bagcolor:rightcolor width:rightwidth isText:rightIsText];
rightView.menuviewDelegate=self;
rightView.hidden=YES;
[self addSubview:rightView]; }
if (hasLeftMenu) {
leftview=[[menuview alloc]initWithFrame:CGRectMake(, , leftwidth*leftconn.count,height)];
[leftview initMenu:leftconn bagcolor:leftcolor width:leftwidth isText:leftIsText];
leftview.menuviewDelegate=self;
leftview.hidden=YES;
[self addSubview:leftview];
}
self.userInteractionEnabled=YES;
self.contentView.userInteractionEnabled=YES;
self.contentView.backgroundColor=[UIColor whiteColor]; UISwipeGestureRecognizer * sc1=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
sc1.delegate=self;
[sc1 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer * sc2=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
sc2.delegate=self;
[sc2 setDirection:UISwipeGestureRecognizerDirectionRight];
[self.contentView addGestureRecognizer:sc1];
[self.contentView addGestureRecognizer:sc2];
[self bringSubviewToFront:self.contentView]; } - (void) btnClick:(NSInteger)index
{
[self.myTableViewCellDelegate btnMenuClick:index isRightMenu:state]; } -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 判断是不是UIButton的类
if ([touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
else
{
return YES;
}
} -(void)handleSwipe:(UISwipeGestureRecognizer *)gesture { switch (gesture.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"%@",@"up");
break; case UISwipeGestureRecognizerDirectionDown: break; case UISwipeGestureRecognizerDirectionLeft:
{
if (state==CellStateCenter) { rightView.hidden=NO;
CGRect rct=CGRectMake( self.contentView.frame.origin.x , self.contentView.frame.origin.y,
self.contentView.frame.size.width, self.contentView.frame.size.height);
rct.origin.x=rct.origin.x-rightwidth*[rightconn count];
self.contentView.frame=rct;
state=CellStateRight;
} if (state==CellStateLeft) {
leftview.hidden=YES;
CGRect rct=CGRectMake( self.contentView.frame.origin.x , self.contentView.frame.origin.y,
self.contentView.frame.size.width, self.contentView.frame.size.height);
rct.origin.x=rct.origin.x-leftwidth*[leftconn count];
self.contentView.frame=rct;
state=CellStateCenter;
} }
break;
case UISwipeGestureRecognizerDirectionRight:
{ if (state==CellStateCenter) {
leftview.hidden=NO;
CGRect rct=CGRectMake( self.contentView.frame.origin.x , self.contentView.frame.origin.y,
self.contentView.frame.size.width, self.contentView.frame.size.height);
rct.origin.x=rct.origin.x+leftwidth*[leftconn count];
self.contentView.frame=rct;
state=CellStateLeft;
} if (state==CellStateRight) {
rightView.hidden=YES;
CGRect rct=CGRectMake( self.contentView.frame.origin.x , self.contentView.frame.origin.y,
self.contentView.frame.size.width, self.contentView.frame.size.height);
rct.origin.x=rct.origin.x+rightwidth*[rightconn count];
self.contentView.frame=rct;
state=CellStateCenter;
} }
break; default:
break;
} }
- (void) RightViewSelectindex:(UIButton *) btn
{
NSLog(@"%ld",btn.tag);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end
5.说明
开发环境是Xcode 6.1 SDK8.1 未测试其他版本的效果,如果需要测试dome留下邮箱,我会发给你。
欢迎转载。
iOS Swipe Tableviewcell(左右滑动出现按钮)的更多相关文章
- iOS之UITableView带滑动操作菜单的Cell
制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...
- Ionic 2 :如何实现列表滑动删除按钮
http://blog.csdn.net/h254532699/article/details/54382123 使用Ionic这种框架伟大的地方在于用户界面元素默认准备好了,意味着你可以设计更好的a ...
- IOS的H5页面滑动不流畅的问题:
IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK
- Android中Touch事件分析--解决HorizontalScrollView滑动和按钮事件触发问题
之前写过关于HorizontalScrollView滑动和按钮事件触发问题,但是不能所有的情况,最近几天一直在想这个问题,今天有一个比较好的解决思路,最终应用在项目里面效果也很好,首先说明一下功能: ...
- iOS设置拍照retake和use按钮为中文简体
iOS设置拍照retake和use按钮为中文简体,设置有两种方式一个是代码直接控制,第二就是xcode配置本机国际化为“china”(简体中文). 本文重点要说的是第二种,这样配置有两个好处,一是操作 ...
- h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽(露出黑色背景)
h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽 标签: 手机 2016-02-02 18:09 696人阅读 评论(0) 收藏 举报 在ios下,双击屏幕某些地方,滚动条会自动向上走一段. ...
- iOS实现tableViewCell或collectionCell中点击界面按钮跳转
//找到父类界面 - (UIViewController *)viewController { for (UIView* next = [self superview]; next; next = n ...
- 再谈iOS 7的手势滑动返回功能
本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147 之前随手写过一篇<使用UIScreenEdgePanGestureR ...
- iOS下简单实现滑动导航条
功能介绍 最近在做一款ios的app,其中有一个页面需要分成三个版块,版块之间可以通过左右滑动来进行切换,也可以通过点击上方的按钮来切换,好像在android中可以用过ViewPager + Frag ...
随机推荐
- C++设计模式-Factory工厂模式
Factory1.定义创建对象的接口,封装对象的创建2.将实际创建工作延迟到子类中,例如,类A中药使用类B,B是抽象父类,但是在类A中不知道具体要实例化哪一个B的子类,但是在类A的子类D中是可以知道的 ...
- java之注解Annotation
元注解:负责注解其他注解,java5提供的4个meta-annotation元注解 @Target 规定注解修饰的范围 ElementType.CONSTRUCTOR:构造器声明 ElementTyp ...
- jsp 错误码debug记录与总结
500: 编码错误: 无法向cookie中写入中文字符串 需要使用URLEncoder.Encode()在写入处进行转码,使用URLDecoder.decoder()在读取处进行解码 或者使用requ ...
- PIC32MZ tutorial -- Hello World
Today I implement "Hello World" on PIC32MZ EC starter kit. The application of "Hello ...
- java 的SYSTEM类【转】
java 的SYSTEM类[转] Posted on 2009-12-03 16:46 火之光 阅读(728) 评论(0) 编辑 收藏 System类代表系统,系统级的很多属性和控制方法都放置在该类的 ...
- —linux 磁盘配额按用户管理(quota)
我根据下面的ref链接整理的基本是的按用户额度管理步骤 (按组的额度管理被简化掉) 我在Ubuntu服务器12.04下整理,其他版本的Ubuntu和Linux应该都没有问题的 (有任何错误都指正给我, ...
- linux C学习笔记01--makefile
不知不觉毕业五年了,以前学的linux基本都忘了,重新温习起来吧! 下面是自己写的makefile文件,供新手和自己回头时查阅 CC=gcc EXE=c.out CCC=g++ EEE=cc.out ...
- mininet之miniedit可视化操作
Mininet 2.2.0之后的版本内置了一个mininet可视化工具miniedit,使用Mininet可视化界面方便了用户自定义拓扑创建,为不熟悉python脚本的使用者创造了更简单的环境,界面直 ...
- framebuffer应用编程实践
framebuffer的使用主要包括4个部分: (1):首先需要打开设备文件 /dev/fb0. (2):获取设备的信息.包括可变信息和不可变信息,分别使用两个结构体来进行封装,这两个结构体在 < ...
- python sort和sorted的区别以及使用方法
iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...