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. The listener supports no services解决一例

    The listener supports no services解决一例   Listener动态监听静态监听注册实例 今天做Advacned Replication实验的时候碰到一个问题,启动目标 ...

  2. Testin云测试平台初体验

    这几天偶然接触到了一个叫做Testin的云测试平台,经过一番体验,感觉还是不错的,因为里面提供了大量的测试机型,可以针对Android手机的严重碎片化现象做出比较全面的测试,同时Testin的测试内容 ...

  3. c# 串口编程

    http://news.ccidnet.com/art/32859/20100524/2067861_4.html 字节缓冲器处理类: /// <summary> /// 字节缓冲器 // ...

  4. 【练习】移动数据----infile *

    要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...

  5. app的meta标签

    1.规定网页编码 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> ...

  6. win10 Administrator没有管理员权限解决方案

    方法/步骤 在运行框 里面输入“gpedit.msc”. 打开组策略编辑器,计算机配置->windows设置->安全设置->->本地策略->安全选项 找到“用户账户控制: ...

  7. div高度自适应(父元素未知,所有高度跟随子元素最大的高度)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. NK3C程序配置

    1.坐席软电话 1)NKZXAgent 1)需要环境:.netframework 4.0 2)reg.bat 注册:(确认注册成功) 3)TestAgt.exe 软电话签入测试 LoadConfig: ...

  9. ajax执行完成后,再执行下面的代码的解决办法

    一般ajax设置的都是异步的,但是有时候我们有这种需求,就是等ajax执行完成之后,在执行下面的函数. 1设置async:false 在jq中直接设置了ajax是异步的还是同步的 一般如果不写这个,默 ...

  10. Java、Android 开发环境搭建

    一.准备工作 为便于管理,将java开发工具集中到一个文件夹中.创建D:\javaDevE文件夹,JDK.Android-SDK.Eclipse.tomcat等都可以安装到这个文件夹中. 二.搭建Ja ...