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(左右滑动出现按钮)的更多相关文章

  1. iOS之UITableView带滑动操作菜单的Cell

    制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...

  2. Ionic 2 :如何实现列表滑动删除按钮

    http://blog.csdn.net/h254532699/article/details/54382123 使用Ionic这种框架伟大的地方在于用户界面元素默认准备好了,意味着你可以设计更好的a ...

  3. IOS的H5页面滑动不流畅的问题:

    IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK

  4. Android中Touch事件分析--解决HorizontalScrollView滑动和按钮事件触发问题

    之前写过关于HorizontalScrollView滑动和按钮事件触发问题,但是不能所有的情况,最近几天一直在想这个问题,今天有一个比较好的解决思路,最终应用在项目里面效果也很好,首先说明一下功能: ...

  5. iOS设置拍照retake和use按钮为中文简体

    iOS设置拍照retake和use按钮为中文简体,设置有两种方式一个是代码直接控制,第二就是xcode配置本机国际化为“china”(简体中文). 本文重点要说的是第二种,这样配置有两个好处,一是操作 ...

  6. h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽(露出黑色背景)

    h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽 标签: 手机 2016-02-02 18:09 696人阅读 评论(0) 收藏 举报   在ios下,双击屏幕某些地方,滚动条会自动向上走一段. ...

  7. iOS实现tableViewCell或collectionCell中点击界面按钮跳转

    //找到父类界面 - (UIViewController *)viewController { for (UIView* next = [self superview]; next; next = n ...

  8. 再谈iOS 7的手势滑动返回功能

    本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147  之前随手写过一篇<使用UIScreenEdgePanGestureR ...

  9. iOS下简单实现滑动导航条

    功能介绍 最近在做一款ios的app,其中有一个页面需要分成三个版块,版块之间可以通过左右滑动来进行切换,也可以通过点击上方的按钮来切换,好像在android中可以用过ViewPager + Frag ...

随机推荐

  1. android Tab 类型切换界面

    实现方案:viewpager + fragment + FragmentPagerAdapter 效果图: 可以左右滑动切换选项卡,或者点击: 如果想使用fragment的时候又想可以左右滑动,就可以 ...

  2. springMVC下载FTP上的文件

    springMVC下载FTP上的文件 今天没时间写.先上传 一个工具类 工具类 package com.utils; import java.io.File; import java.io.FileO ...

  3. Hibernate总结

    SSH原理总结 Hibernate工作原理及为什么要用: 原理: hibernate,通过对jdbc进行封装,对 java类和 关系数据库进行mapping,实现了对关系数据库的面向对象方式的操作,改 ...

  4. web服务器长连接

    web服务器都提供长连接的方式,所谓长连接就是客户端一次请求完后,不关闭连接,保持一段时间的连接,下次此客户端再次请求时,不用创建新连接,复用所保持的连接即可.从理论上,长连接可以免去大量建立和关闭连 ...

  5. Python之路【第十九篇】:爬虫

    Python之路[第十九篇]:爬虫   网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用 ...

  6. java并发编程_CountDownLanch(倒计数锁存器)应用场景

    使用介绍: 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在 ...

  7. Android之使用Android-AQuery异步加载图片(一)

    第一节:转载地址(http://www.cnblogs.com/lee0oo0/archive/2012/10/25/2738299.html) // 必须实现AQuery这个类 AQuery aq ...

  8. WebClient异步下载文件

    namespace ConsoleAppSyncDownload{    class Program    { static void Main(string[] args)        {     ...

  9. EAS linux挂载数据盘

    查看数据盘名称 fdisk -l 假设没有挂载的数据盘为/dev/xvdb 格式化数据盘 mkfs.ext3 /dev/xvdb 添加自动挂载 mkdir /data echo '/dev/xvdb ...

  10. 解决方法:An error occurred on the server when processing the URL. Please contact the system administrator

    在WINDOWS7或SERVER2008上安装了IIS7.5,调试ASP程序时出现以下错误: An error occurred on the server when processing the U ...