ios 拖动重新排序
2016-12-09 17:10
387 查看
最近项目需要,做了拖动重新排序的功能
此为系统的方法iOS9之后可用,如果需要兼容9.0之前的版本请,网上找demo(挺多的,也封装的挺好的)
效果图如:
- (void)setMycollectionView{
self.MyCollectionView.delegate =
self;
self.MyCollectionView.dataSource =
self;
self.MyCollectionView.pagingEnabled=NO;
self.MyCollectionView.showsVerticalScrollIndicator=NO;
self.MyCollectionView.showsHorizontalScrollIndicator=NO;
self.MyCollectionView.scrollEnabled=NO;
self.MyCollectionView.scrollsToTop=NO;
//deviceSystenVersion 判断系统的版本号
if (deviceSystenVersion>8.9) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(onLongPressed:)];
[self.MyCollectionView
addGestureRecognizer:longPress];
}
}
- (void)onLongPressed:(UILongPressGestureRecognizer *)sender {
CGPoint point = [sender
locationInView:sender.view];
//根据点击的手势获取试图的NSIndexPath
NSIndexPath *indexPath = [self.MyCollectionView
indexPathForItemAtPoint:point];
switch (sender.state) {
case
UIGestureRecognizerStateBegan: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case
UIGestureRecognizerStateChanged: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
updateInteractiveMovementTargetPosition:point];
}
break;
}
case
UIGestureRecognizerStateEnded: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
endInteractiveMovement];
}
break;
}
default: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
cancelInteractiveMovement];
}
break;
}
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
UICollectionViewCell * cellView;
self.titleArray=[NSMutableArray
array];
//次数组存储图片名
[self.titleArray
addObjectsFromArray:@[@"pic_shangjia",@"pic_shangjia",@"pic_shangjia"]];
CompileCollectionCell * cccell=[collectionView
dequeueReusableCellWithReuseIdentifier:@"CompileCollectionCell"
forIndexPath:indexPath];
if (indexPath.row<self.titleArray.count)
{
[cccell.ImageHeaderView
setImage:[UIImage
imageNamed:[self.titleArray
objectAtIndex:indexPath.row]]];
}
cellView=cccell;
return cellView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return
1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
4;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((SCREEN_WITCH-40)/4,(SCREEN_WITCH-40)
/4);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake((105-(SCREEN_WITCH-40)/4)/2,
5, (150-(SCREEN_WITCH-40)/4)/2,
5);
}
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
//判断系统为8.9(如果打印版本号,会略小,所以使用8.9实际是9.0)和没有图片的不让编辑
if (deviceSystenVersion>=8.9 && indexPath.row<self.titleArray.count)
{
return
YES;
}else
{
return
NO;
}
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath
{
//数据源的调整数据源
[self.titleArray
exchangeObjectAtIndex:sourceIndexPath.item
withObjectAtIndex:destinationIndexPath.item];
}
此为系统的方法iOS9之后可用,如果需要兼容9.0之前的版本请,网上找demo(挺多的,也封装的挺好的)
效果图如:
- (void)setMycollectionView{
self.MyCollectionView.delegate =
self;
self.MyCollectionView.dataSource =
self;
self.MyCollectionView.pagingEnabled=NO;
self.MyCollectionView.showsVerticalScrollIndicator=NO;
self.MyCollectionView.showsHorizontalScrollIndicator=NO;
self.MyCollectionView.scrollEnabled=NO;
self.MyCollectionView.scrollsToTop=NO;
//deviceSystenVersion 判断系统的版本号
if (deviceSystenVersion>8.9) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(onLongPressed:)];
[self.MyCollectionView
addGestureRecognizer:longPress];
}
}
- (void)onLongPressed:(UILongPressGestureRecognizer *)sender {
CGPoint point = [sender
locationInView:sender.view];
//根据点击的手势获取试图的NSIndexPath
NSIndexPath *indexPath = [self.MyCollectionView
indexPathForItemAtPoint:point];
switch (sender.state) {
case
UIGestureRecognizerStateBegan: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case
UIGestureRecognizerStateChanged: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
updateInteractiveMovementTargetPosition:point];
}
break;
}
case
UIGestureRecognizerStateEnded: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
endInteractiveMovement];
}
break;
}
default: {
if (indexPath.row<self.titleArray.count)
{
[self.MyCollectionView
cancelInteractiveMovement];
}
break;
}
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
UICollectionViewCell * cellView;
self.titleArray=[NSMutableArray
array];
//次数组存储图片名
[self.titleArray
addObjectsFromArray:@[@"pic_shangjia",@"pic_shangjia",@"pic_shangjia"]];
CompileCollectionCell * cccell=[collectionView
dequeueReusableCellWithReuseIdentifier:@"CompileCollectionCell"
forIndexPath:indexPath];
if (indexPath.row<self.titleArray.count)
{
[cccell.ImageHeaderView
setImage:[UIImage
imageNamed:[self.titleArray
objectAtIndex:indexPath.row]]];
}
cellView=cccell;
return cellView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return
1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
4;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((SCREEN_WITCH-40)/4,(SCREEN_WITCH-40)
/4);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake((105-(SCREEN_WITCH-40)/4)/2,
5, (150-(SCREEN_WITCH-40)/4)/2,
5);
}
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
//判断系统为8.9(如果打印版本号,会略小,所以使用8.9实际是9.0)和没有图片的不让编辑
if (deviceSystenVersion>=8.9 && indexPath.row<self.titleArray.count)
{
return
YES;
}else
{
return
NO;
}
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath
{
//数据源的调整数据源
[self.titleArray
exchangeObjectAtIndex:sourceIndexPath.item
withObjectAtIndex:destinationIndexPath.item];
}
相关文章推荐
- xamarin.ios 拖动tablecell来重新排序
- IOS UITableView拖动排序功能
- jQuery拖动元素并对元素进行重新排序
- Flex中如何通过dragEnabled, dropEnabled和dragMoveEnabled属性,使Tree中的节点可以被拖动重新排序
- IOS UITableView拖动排序功能
- 【推荐】iOS集合视图的可重新排序的layout
- iOS 9的新特性之一——实现给collectionView上的cell进行重新排序的功能
- iOS拖动按钮排序UI效果
- iOS UITableView的细致功能(滑动删除,拖动排序,添加行,搜索过滤)
- iOS UITableView 拖动排序实现代码
- jQuery拖动元素并对元素进行重新排序
- iOS 删除、重新排序xcdatamodel
- jQuery对元素进行拖动并重新排序代码
- IOS UITableView拖动排序功能
- IOS UITableView拖动排序功能
- iOS UITableView(四)-Cell拖动更改排序
- 用鼠标拖动表格进行排序.
- 请教重新排序的问题
- XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子
- XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子