您的位置:首页 > 移动开发 > IOS开发

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息