ios 拖动重新排序

最近项目需要,做了拖动重新排序的功能

此为系统的方法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];

}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值