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]通过js方法回调部分native报错 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

    在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...

  2. 关于yaha中文分词(将中文分词后,结合TfidfVectorizer变成向量)

    https://github.com/jannson/yaha # -*- coding: utf-8 -*- """ Created on Wed Aug 10 08: ...

  3. Python 前端之JS

    JavaScript由浏览器编译运行 JS的导入方式有两种,一种直接定义,第二种通过src引入:可以存放在<head>头部,但是强烈建议放在<body>的最下面,因为如果你引入 ...

  4. awk用法

    目前虽然有很多工具可以代替awk,但是呢我还是认为awk还是非常重要,比如有时候load数据到hive,mysql发现数据有点问题,这样可以先对比文件和库中数据是否一致,这样awk就发挥用处了,还有从 ...

  5. iOS崩溃日志记录工具--CrashlyTics

    http://try.crashlytics.com Crashlytics优势: 1.Crashlytics基本不会漏掉任何应用崩溃的信息 2.Crashlytics对崩溃日志管理很人性化,会根据崩 ...

  6. 用定时器令P0(或其它IO口)产生多路方波

    void Timer0_isr(void) interrupt 1 using 1{ static unsigned char i;  //重新赋值 12M晶振计算,指令周期1uS,500x2=1mS ...

  7. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  8. coursera 机器学习课程 GraphLab环境准备

    在网上看到coursera有机器学习的课程,正好再学习学习,温固一下,还有很多其他的课程也很好.收费的哟! 手机APP和网站收取的费用有差异,网站上要便宜一下,费用差的挺多的,果断在网站上支付了. 有 ...

  9. Scrum会议10.19

    Scrum会议 组名称:好好学习 项目名称:记账本 参会成员:林莉(Master)胡丽娜 宫丽君 汪东涵 时间:2016.10.19 已完成内容: 1.完成新项目的查找,查找学姐的代码和项目. 2.理 ...

  10. JVM调优-关于jvm的一些基本概念

    1.数据类型 java体系中,数据类型可分为2类:基本类型和引用类型.基本类型保存变量原始值,即:他代表的值就是数值本身: 而引用类型的变量保存引用值."引用值"代表某个对象的引用 ...