iOS 学习 - 20 UICollectionView 移动 Item ,类似背包
有100个 item,数据源只有20个,只能在 20 个之间移动,防止 item 复用,出现 bug
方法一:苹果自带
//UICollectionViewDataSource
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;
方法二:
1.获取要拖拽的 Item
2.使用系统自带方法截图,隐藏当前 Item
3.交换位置、数据位置
#pragma mark -- 手势开始
- (void)xwp_gestureBegan:(UILongPressGestureRecognizer *)longPressGesture{
CGFloat y = [longPressGesture locationInView:longPressGesture.view].y;
//超过数据源的 item 不执行
if (y <= [self row]) {
//获取手指所在的cell
_originalIndexPath = [self indexPathForItemAtPoint:[longPressGesture locationOfTouch: inView:longPressGesture.view]];
UICollectionViewCell *cell = [self cellForItemAtIndexPath:_originalIndexPath];
//使用系统自带方法截图
UIView *tempMoveCell = [cell snapshotViewAfterScreenUpdates:NO];
//隐藏当前 Item
cell.hidden = YES;
_tempMoveCell = tempMoveCell;
_tempMoveCell.frame = cell.frame;
[self addSubview:_tempMoveCell]; //开启边缘滚动定时器
[self xwp_setEdgeTimer];
//开启抖动
if (_shakeWhenMoveing && !_editing) {
[self xwp_shakeAllCell];
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
_lastPoint = [longPressGesture locationOfTouch: inView:longPressGesture.view];
//通知代理
if ([self.delegate respondsToSelector:@selector(dragCellCollectionView:cellWillBeginMoveAtIndexPath:)]) {
[self.delegate dragCellCollectionView:self cellWillBeginMoveAtIndexPath:_originalIndexPath];
}
}
}
#pragma mark -- 手势抖动
- (void)xwp_gestureChange:(UILongPressGestureRecognizer *)longPressGesture{
//获取点击位置 y 值
CGFloat y = [longPressGesture locationInView:longPressGesture.view].y;
//限制手势范围 y 值
if (y <= [self row]) {
NSLog(@"-----%f",y);
//通知代理
if ([self.delegate respondsToSelector:@selector(dragCellCollectionViewCellisMoving:)]) {
[self.delegate dragCellCollectionViewCellisMoving:self];
}
CGFloat tranX = [longPressGesture locationOfTouch: inView:longPressGesture.view].x - _lastPoint.x;
CGFloat tranY = [longPressGesture locationOfTouch: inView:longPressGesture.view].y - _lastPoint.y;
_tempMoveCell.center = CGPointApplyAffineTransform(_tempMoveCell.center, CGAffineTransformMakeTranslation(tranX, tranY));
_lastPoint = [longPressGesture locationOfTouch: inView:longPressGesture.view];
[self xwp_moveCell];
}else{
//如果超过范围,就把当前拖动的 item y 值设为数据源的底部
_tempMoveCell.frame = CGRectMake(_tempMoveCell.frame.origin.x, [self row], _tempMoveCell.frame.size.width, _tempMoveCell.frame.size.height);
}
}
#pragma mark -- 手势取消或者结束
- (void)xwp_gestureEndOrCancle:(UILongPressGestureRecognizer *)longPressGesture{
UICollectionViewCell *cell = [self cellForItemAtIndexPath:_originalIndexPath];
self.userInteractionEnabled = NO;
[self xwp_stopEdgeTimer];
//通知代理
if ([self.delegate respondsToSelector:@selector(dragCellCollectionViewCellEndMoving:)]) {
[self.delegate dragCellCollectionViewCellEndMoving:self];
}
//一个小动画
[UIView animateWithDuration:0.25 animations:^{
_tempMoveCell.center = cell.center;
} completion:^(BOOL finished) {
[self xwp_stopShakeAllCell];
[_tempMoveCell removeFromSuperview];
cell.hidden = NO;
self.userInteractionEnabled = YES;
}];
}
完整 demo,放在 githud 上,点我
CocoaChina 上在这里
iOS 学习 - 20 UICollectionView 移动 Item ,类似背包的更多相关文章
- iOS学习20之UIView
1. UI编程概述 UI的本意是用户界面,是英文 User 和 Interface 的缩写. UI设计则是指对软件的人机交互.操作逻辑.界面美观的整体设计. 好的UI设计不仅是让软件变得有个性有品位, ...
- ios学习之UISwipeGestureRecognizer手势识别
ios学习之UISwipeGestureRecognizer手势识别 本文部分转自俺是一个瓜娃!!!的博客UISwipeGestureRecognizer ---手指动作,转载过来仅是为了自己查询 ...
- 【转】 iOS学习之sqlite的创建数据库,表,插入查看数据
原文: http://blog.csdn.net/totogo2010/article/details/7702207 iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsql ...
- iOS学习——iOS项目Project 和 Targets配置详解
最近开始学习完整iOS项目的开发流程和思路,在实际的项目开发过程中,我们通常需要对项目代码和资料进行版本控制和管理,一般比较常用的SVN或者Github进行代码版本控制和项目管理.我们iOS项目的开发 ...
- iOS学习之sqlite的创建数据库,表,插入查看数据
目录(?)[-] 新建项目sqliteDemo添加使用sqlite的库libsqlite3dylib sqlite 的方法 获取沙盒目录并创建或打开数据库 创建数据表 插入数据 查询数据库并打印数据 ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
- iOS学习之第二个View使用UITabBarViewController
前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之 ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
随机推荐
- 追根溯源:EntityFramework 实体的状态变化
阅读目录: 1. 应用场景 2. 场景测试 3. 问题分析 4. 追根溯源 5. 简要总结 1. 应用场景 首先,应用程序使用 EntityFramework,应用场景中有两个实体 S_Class(班 ...
- eclipse导入项目Archive for required library cannot be read or is not a valid ZIP file
原因 :部分文件毁坏. 解决办法:1. 在eclipse中运行maven clean install 2. 报错,找到报错的文件物理删除,然后重新运行maven clean install 3. 循 ...
- objective-c 语法快速过(7)编译器特性ARC
ARC(是编译器特性) ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内 ...
- 使用Unified Communications Managed API获取Lync在线会议的链接地址
最近在项目上遇到一个问题,需要能够在程序中获取Lync会议的链接地址.Lync是微软出品的一套即时通信(IM)客户端软件,配合Microsoft Lync Server使用,其前身是Microsoft ...
- 【十大经典数据挖掘算法】Apriori
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 关联分析 关联分析是一类非常有 ...
- 《C#并发编程经典实例》笔记
1.前言 2.开宗明义 3.开发原则和要点 (1)并发编程概述 (2)异步编程基础 (3)并行开发的基础 (4)测试技巧 (5)集合 (6)函数式OOP (7)同步 1.前言 最近趁着项目的一段平稳期 ...
- Hibernate ——二级缓存
一.Hibernate 二级缓存 1.Hibernate 二级缓存是 SessionFactory 级别的缓存. 2.二级缓存分为两类: (1)Hibernate内置二级缓存 (2)外置缓存,可配置的 ...
- 实体之间的关系【Entity Relationships】(EF基础系列篇9)
Here, you will learn how entity framework manages the relationships between entities. Entity framewo ...
- 【WP8】WebBrowser相关
2014年09月02日更新 今天用了一下WebBrowser,在使用过程中也遇到了一些问题,在这里做一下记录 虽然WebBrowser比较重,会比较影响性能(除非一定要用到它,否则尽量少用),但有时候 ...
- PetaPoco4.0的事务为什么不会回滚
using (var srop=DbHelper.CurrentDb.GetTransaction()) { ID = bp.AddModel(model).ToStr(); #region 参与楼盘 ...