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& ...
随机推荐
- iOS 10 创建iMessage App
原文作者 澳大利亚19岁少年--Davis Allie ----原文地址 时值中秋佳节, 送给出门在外的程序猿们 ! 骚年们, 自己写个表情包斗图可否 ! 斗-seal.svg.png 第一. 介绍 ...
- 手机浏览器下IScroll中click事件
产品的h5页面几乎都使用了iscroll插件,如果a标签在iscroll里,在部分手机浏览器中会出现无法点击的情况,不管是绑定click事件还是使用a标签的href属性.href属性偶尔还会能点击,c ...
- 深究带PLL的错误复位设计
PLL复位通常犯的错误 或者是像上一篇文章 FPGA知识大梳理(四)FPGA中的复位系统大汇总 中的图一样,也是错误设计.为何呢?看ALTPLL (Phase-Locked Loop) IP Cor ...
- samba服务器上文件名大小写
samba服务器上文件名大小写 如果给HP_UX配置samba之后,通过windows访问有时候会发现文件名大小写不对时,请注意下述配置信息是否正确.在/etc/opt/samba/smb.conf中 ...
- DropDownList控件学习
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- android天气查询(一)websevice之ksoap2软件包的使用
对于用到天气信息,首先我想: 第一:数据不可能是我测得的,必须是网上的信息. 第二:网上的信息分为好多种,具体哪种比较好一点,这里我总结了两种. 第三:数据JSON怎么解析. 第四:如何提出数据与显示 ...
- c++实现精确计时
//获取比較准确是程序执行时间 #include<iostream> #include<windows.h> using namespace std; int main(voi ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Lamd表达式
1. 普通绑定: public void button1_Click(object sender, EventArgs e) { MessageBox.Show("ok"); } ...
- Node.js入门-Node.js 介绍
Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...