iOS之UITableViewCell左右滑动效果
首先在 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左右滑动效果的更多相关文章
- iOS之UITableView带滑动操作菜单的Cell
制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...
- IOS中UITableViewCell使用详解
IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...
- IOS的H5页面滑动不流畅的问题:
IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK
- a 锚点跳转滑动效果
点击a链接时,跳转到相应id的位置处,有一个滑动效果. <a href="#my">我是跳转到div</a><div id="my" ...
- Android Scroll分析——滑动效果产生
相对于在Android2.x版本上出现的长按.点击事件的效果,不得不说,滑动操作具有更好的用户体验.因此,从Android 4.X版本开始,出现了更多滑动操作的效果.越来越多第三方应用模仿这样的效果, ...
- XE8 for iOS 状态栏的几种效果
XE8 实现 iOS 状态栏的几种效果: 一.状态栏底色: 开一个新工程. 设定 Fill.Color 颜色属性. 设定 Fill.Kind = Solid. 无需修改任何官方源码. 二.隐藏状态栏( ...
- iOS - 用 UIBezierPath 实现果冻效果
最近在网上看到一个很酷的下拉刷新效果(http://iostuts.io/2015/10/17/elastic-bounce-using-uibezierpath-and-pan-gesture/). ...
- jquery左右滑动效果的实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件
bootstrap能否实现 手机端滑动效果,滑动到下一页 jgestures.js插件可以解决,只需要引入一个JS文件<script src="js/jgestures.min.js& ...
随机推荐
- Extjs 3.4 生成button,并調用相同的window
/////定義一個方法,用來調用win_mucangjieshou的窗口 var panel_contant= function(id_name){ var aa=Ext.getCmp(id_name ...
- HDU OJ 5437 Alisha’s Party 2015online A
题目:click here 题意: 邀请k个朋友,每个朋友带有礼物价值不一,m次开门,每次开门让一定人数p(如果门外人数少于p,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每 ...
- HOOK API (一)——HOOK基础+一个鼠标钩子实例
HOOK API (一)——HOOK基础+一个鼠标钩子实例 0x00 起因 最近在做毕业设计,有一个功能是需要实现对剪切板的监控和进程的防终止保护.原本想从内核层实现,但没有头绪.最后决定从调用层入手 ...
- BZOJ 2424: [HAOI2010]订货(最小费用最大流)
最小费用最大流..乱搞即可 ------------------------------------------------------------------------------ #includ ...
- Android 开发笔记“Application 理解”
Android 中Application类用法 1. Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时 ...
- InfoQ文章
http://www.infoq.com/cn/presentations/log-platform-construction-weipinhui https://github.com/Telesco ...
- ImageMagick还是GraphicsMagick?
引自:http://co63oc.blog.51cto.com/904636/328997 ImageMagick(IM) 套装包含的命令行图形工具是一主要自由软件:Linux,其他类Unix操作系统 ...
- Mysql-udf-http 插件的安装与使用
1. 在Linux系统上安装Mysql-udf-http ulimit -SHn wget http://curl.haxx.se/download/curl-7.21.1.tar.gz .tar ...
- perl lwp关闭ssl校验
use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Encode; use File:: ...
- BZOJ 1008 越狱 (组合数学)
题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...