首先在 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. SqlCacheDependency的使用

    最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...

  2. sysctl: command not found

    在安装RedHat5.9时没有在安装时定制软件包,在后面使用sysctl命令时提示: -bash: sysctl: command not found 找了半天原来是还需要安装: rpm -ivh p ...

  3. [Swust OJ 360]--加分二叉树(区间dp)

    题目链接:http://acm.swust.edu.cn/problem/360/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  4. BZOJ 2314: 士兵的放置( 树形dp )

    树形dp... dp(x, 0)表示结点x不放士兵, 由父亲控制: dp(x, 1)表示结点x不放士兵, 由儿子控制: dp(x, 2)表示结点x放士兵. ---------------------- ...

  5. 建造者模式->代码示例

    <?php interface Builder{ public function head(); public function body(); public function foot(); ...

  6. 浏览器 窗口 scrollTop 的兼容性问题

    window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...

  7. ThinkPHP常量参考

    常用常量 APP_NAME 当前项目名称 APP_PATH 当前项目路径 GROUP_NAME 当前分组名称 MODULE_NAME 当前Action模块名称 ACTION_NAME 当前操作的名称 ...

  8. ftpclient卡死问题

    ftpclient在调用retrieveFileStream(String remote)之后,返回inputstream,如果不想关闭ftp,继续读取其他文件. 一定要先关闭inputstream, ...

  9. wireshark 包分析命令

    1.查看原地址过滤包命令: ip.src ==192.168.1.1 2.查看目的地址过滤包:ip.dst == 192.168.1.1 3.关键字 eq 等于 "==" ,and ...

  10. Output in PowerShell

    Reference article: https://rkeithhill.wordpress.com/2007/09/16/effective-powershell-item-7-understan ...