自定义拖拽cell
2016-07-06 20:34
706 查看
UITableView自带的 move 效果需要拖特定的地方才能移动,有时并不方便。所以研究下拖任意部位拖走cell的方法
上图为一个横着的
tableView,其中的图片可以长按之后拖走
思路为:隐藏选中的
cell,用
UIImageView来代替它,然后被拖来拖去。拖动的过程中如果到了一个新的位置,则更新
dataSouce,并重新加载
tableView,直至松手。松手后,移除替身
UIImageView,正常显示所有的
cell即可。
实现的步骤为:
设置一个用来保存被拖动的
cell位置的变量
editingIndexPath,初始为
nil。在生产
cell的过程中,如果
editingIndexPath不为空,则隐藏该位置的
cell
设置一个用来保存上次手指所在位置的变量
lastIndexPath
为
tableView添加
UILongPressGestureRecognizer
在手势的
state为
UIGestureRecognizerStateBegan时:(刚按下去)
使用
[recognizer locationInView:tableView];得到按下的坐标
使用
[tableView indexPathForRowAtPoint:point];得到该坐标所在的
indexPath
将
editingIndexPath与
lastIndexPath设置为
indexPath,重新加载
tableView
得到该
cell的快照
imageView(本例中
cell本身就是图片),并设置其
center
在手势的
state为
UIGestureRecognizerStateChanged时:(拖动)
获取新的坐标,设置
imageView的
center,使其实时随手移动
计算新的
indexPath,判断是不是和
lastIndexPath相等,如果不相等,说明图片拖到了一个新的位置,此时需要将
dataSource中两个位置的数据进行交换,令
editingIndexPath = lastIndexPath,并重新加载
tableView
更新
lastIndexPath
在手势的
state为
UIGestureRecognizerStateEnded时:(松手)
将
imageView移除
将
editingIndexPath设为
nil,并重新加载
tableView
移动到边上
tableView滚动、点到
headerView上无效等等这些细节略过
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- 在 Linux 中如何移动文件
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- PowerShell移动目录中指定文件的方法(非全部文件)
- 鼠标触发移动的分层菜单 层菜单moveMenu
- 基于Android实现随手指移动的ImageView
- php实现复制移动文件的方法
- javascript 广告移动特效的实现代码
- javascript实现图片跟随鼠标移动效果的方法
- JavaScript Select和Option列表元素上下左右移动
- JavaScript代码实现左右上下自动晃动自动移动
- JS实现网页游戏中滑块响应鼠标点击移动效果
- JS+CSS实现分类动态选择及移动功能效果代码
- 详解jQuery移动页面开发中的ui-grid网格布局使用
- jquery移动点击的项目到列表最顶端的方法