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 ...
随机推荐
- Java面向对象的思想
面向对象的思想 1.面向对象的思想的特点: ①符合人们的日常思考习惯 ②能将复杂的问题简单化 ③将原来的执行者变为了现在的指挥者 面向对象的思想,所谓的对象,其实就是实体.对于实物的描述,通常有两个方 ...
- ssh通道技术
所有机器均为Linux操作系统. 机器是A,中间服务器为B,目标服务器是C. 从A可以ssh到B,从B可以ssh到C,但是A不能直接ssh到C. 现在展示利用ssh通道技术从A直接传输文件到C. ...
- android通过gradle打包
这里是最简单的打包方法,实际上gradle的语法是groovy,可以通过编写脚本实现更智能的构建,这个我还不懂==,等我学习了解后,单独整理一个gradle的随笔,这里先应付打包吧 环境要求 安装 ...
- 常用JS汇总
01. 取文档url参数值 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + &q ...
- 调用xml文件的bean
AcTest.class package com.zyz.db; import com.zyz.dao.PersonDao; import org.springframework.context.Ap ...
- iOS button文字居中
新建一个UIButton的category .h @interface UIButton (QXTitleInCenter) -(instancetype)init; @end .m @impleme ...
- Linux网络编程-SIGPIPE信号导致的程序退出问题
当客户端close关闭连接时,若server端接着发送数据,根据TCP协议的规定,server端会收到RST响应,当server端再次往客户端发送数据时,系统会发出一个SIGPIPE信号给server ...
- Light OJ 1025 - The Specials Menu(动态规划-区间dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 题目大意:一串字符, 通过删除其中一些字符, 能够使这串字符变成回文串. ...
- UVA 753 A Plug for UNIX(二分图匹配)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the Unit ...
- iconv命令 gbk 转 UTF-8
-----linux gbk 转 UTF-8-------- iconv 用法 iconv -f "gbk" -t "utf-8" < infile &g ...