首先在 UITableViewCell.h 中声明一个代理

@protocol UITableViewCellSlideDelegate <UITableViewDelegate>

@optional
- (void)tableView:(UITableView *)tableView slideToRightWithIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView slideToLeftWithIndexPath:(NSIndexPath *)indexPath;
@end

然后在 UITableViewCell.m 中 创建一个手势 UIPanGestureRecognizer,因为我们左右滑动的时候必须依赖拖动手势,然后在 UIPanGestureRecognizer 的代理方法中进行判断到底是左滑还是右滑

static const CGFloat kSlideWidth = 80;
static const CGFloat kSlideWidthDelta = 0.08; #pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (self.panGestureRecognizer == gestureRecognizer) {
CGPoint point = [self.panGestureRecognizer translationInView:self];
return fabs(point.x) > fabs(point.y);
} else {
return NO;
}
} #pragma mark - Event Handler
- (void)panGestureRecognizerHandler:(UIPanGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateChanged: {
CGPoint point = [gestureRecognizer translationInView:self];
CGFloat offset = point.x;
if (offset >= kSlideWidth) { offset = kSlideWidth + (offset - kSlideWidth) * kSlideWidthDelta; if (_parent.delegate && [_parent.delegate respondsToSelector:@selector(tableView:slideToRightWithIndexPath:)]) { id<UITableViewCellSlideDelegate> delgate = (id<UITableViewCellSlideDelegate>)_parent.delegate;
[delgate tableView:_parent slideToRightWithIndexPath:_indexPath]; } } else if (offset <= -kSlideWidth) { offset = -kSlideWidth + (offset + kSlideWidth) * kSlideWidthDelta; if (_parent.delegate && [_parent.delegate respondsToSelector:@selector(tableView:slideToLeftWithIndexPath:)]) {
id<UITableViewCellSlideDelegate> delgate = (id<UITableViewCellSlideDelegate>)_parent.delegate;
[delgate tableView:_parent slideToLeftWithIndexPath:_indexPath]; } }
self.contentView.center = CGPointMake(self.center.x + offset,
self.contentView.center.y);
break;
}
default:
break;
}
}

最后就可以在外部调用代理方法进行相关的操作了

#pragma mark - UITableViewCellSlideDelegate
- (void)tableView:(UITableView *)tableView slideToRightWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"向右滑动");
} - (void)tableView:(UITableView *)tableView slideToLeftWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"向左滑动");
}

iOS之UITableViewCell左右滑动效果的更多相关文章

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

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

  2. IOS中UITableViewCell使用详解

    IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...

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

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

  4. a 锚点跳转滑动效果

    点击a链接时,跳转到相应id的位置处,有一个滑动效果. <a href="#my">我是跳转到div</a><div id="my" ...

  5. Android Scroll分析——滑动效果产生

    相对于在Android2.x版本上出现的长按.点击事件的效果,不得不说,滑动操作具有更好的用户体验.因此,从Android 4.X版本开始,出现了更多滑动操作的效果.越来越多第三方应用模仿这样的效果, ...

  6. XE8 for iOS 状态栏的几种效果

    XE8 实现 iOS 状态栏的几种效果: 一.状态栏底色: 开一个新工程. 设定 Fill.Color 颜色属性. 设定 Fill.Kind = Solid. 无需修改任何官方源码. 二.隐藏状态栏( ...

  7. iOS - 用 UIBezierPath 实现果冻效果

    最近在网上看到一个很酷的下拉刷新效果(http://iostuts.io/2015/10/17/elastic-bounce-using-uibezierpath-and-pan-gesture/). ...

  8. jquery左右滑动效果的实现

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

  9. bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件

    bootstrap能否实现 手机端滑动效果,滑动到下一页 jgestures.js插件可以解决,只需要引入一个JS文件<script src="js/jgestures.min.js& ...

随机推荐

  1. TabelView的多选模式

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,st ...

  2. PHPExcel 导出

    <?php include '../init.inc.php'; include "../db.inc.php"; /* @func 引入类 */ include ROOT. ...

  3. maven build脚本笔记

    如果 code 只存在src/java/main 路径下,直接install就好,不必写<build> 资源文件:edu-common-config <build> <f ...

  4. HDU2018-母牛的故事

    描述: 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? 代码: 第n年的牛,等于第n-1年的牛(已有的)+第n-3年 ...

  5. [置顶] 老孟 DB2 V9.7 ESE(一)产品部署 基于centOS 6.4

    本文安装系统CENTOS 6.4 DB2位数64 安装中涉及目录位置各位可自行定义 生产系统为安全和性能考虑,一般将DB2实例目录.日志目录.归档日志目录.表空间目录区分开,可建立/db2home / ...

  6. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...

  7. 我被SQL注入撞了一下腰

    网站的注入漏洞,应该说绝大多数做web开发的人都知道的事情.可是没想到从事6,7年开发工作的我,却会在这上栽了跟头,真是郁闷啊.心情很纠结,按照老婆的话,怎么感觉我像失恋了一样. 事情的起因还是在几个 ...

  8. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  9. VC使用CRT调试功能来检测内存泄漏

    信息来源:csdn     C/C++ 编程语言的最强大功能之一便是其动态分配和释放内存,但是中国有句古话:“最大的长处也可能成为最大的弱点”,那么 C/C++ 应用程序正好印证了这句话.在 C/C+ ...

  10. nginx服务器屏蔽上游错误码

    平时的开发工作中,有时会遇到脚本权限不对导致403,文件被删除导致404,甚至后端业务异常导致5xx等情况,其实我们可以在服务器加上判断,检测当后端服务出现异常的时候前端返回一个指定的静态文件(也可以 ...