利用collectionView实现九宫格移动(参照工商银行手机app首页)

本文介绍了一种在iOS应用中实现九宫格移动的方法,通过在UICollectionView上添加长按手势来实现元素拖动和排序的功能。具体实现包括UICollectionView的布局设置、长按手势的状态管理及响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于九宫格移动的问题,其主要就是在collectionView  上面添加一个长按手势,在实现长按手势的代码里面 主要有3个状态, 一个是长按手势的开启UIGestureRecognizerStateBegan  UIGestureRecognizerStateChanged UIGestureRecognizerStateEnded这三种状态, 把握好在不同的状态下他们的趋势


#import "ViewController.h"
#import "MineCollectionViewCell.h"
#define DEVICE_WIDTH   ([UIScreen mainScreen].bounds.size.width)
#define DEVICE_HEIGHT  ([UIScreen mainScreen].bounds.size.height)
static NSString *collectionCell = @"mineCell";
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *mineCollection;
@property (nonatomic, strong) NSMutableArray *imagesArray;
@property (nonatomic, strong) NSMutableArray *cellAttributesArray;
@property (nonatomic, assign) CGPoint lastPressPoint;

@end

@implementation ViewController

- (NSMutableArray *)imagesArray{
    if (!_imagesArray) {
        self.imagesArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _imagesArray;
}

- (NSMutableArray *)cellAttributesArray{
    if (!_cellAttributesArray) {
        self.cellAttributesArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _cellAttributesArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    _lastPressPoint = CGPointZero;
    self.view.backgroundColor = [UIColor lightGrayColor];
    for (int i = 0; i < 10; i++) {
        [self.imagesArray addObject:[NSString stringWithFormat:@"%d",i+1]];
    }
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 5.0;
    layout.minimumInteritemSpacing = 5.0;
    layout.sectionInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
    _mineCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64.0, DEVICE_WIDTH, DEVICE_HEIGHT - 64.0) collectionViewLayout:layout];
    _mineCollection.backgroundColor = [UIColor lightGrayColor];
    _mineCollection.dataSource = self;
    _mineCollection.delegate = self;
    [_mineCollection registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:collectionCell];
    [self.view addSubview:_mineCollection];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imagesArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((DEVICE_WIDTH - 25.0) / 4.0, (DEVICE_WIDTH - 25.0) / 4.0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.cellImage.image = [UIImage imageNamed:self.imagesArray[indexPath.row]];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
    [cell addGestureRecognizer:longPress];
    return cell;
}

- (void)longPressGesture:(UILongPressGestureRecognizer *)sender{
    MineCollectionViewCell *cell = (MineCollectionViewCell *)sender.view;
    NSIndexPath *cellIndexPath = [_mineCollection indexPathForCell:cell];
    [_mineCollection bringSubviewToFront:cell];
    BOOL isChanged = NO;
    if (sender.state == UIGestureRecognizerStateBegan) {
        [self.cellAttributesArray removeAllObjects];
        for (int i = 0;i< self.imagesArray.count; i++) {
            [self.cellAttributesArray addObject:[_mineCollection layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]];
        }
        self.lastPressPoint = [sender locationInView:_mineCollection];
    }else if (sender.state == UIGestureRecognizerStateChanged){
        cell.center = [sender locationInView:_mineCollection];
        for (UICollectionViewLayoutAttributes *attributes in self.cellAttributesArray) {
            if (CGRectContainsPoint(attributes.frame, cell.center) && cellIndexPath != attributes.indexPath) {
                isChanged = YES;
                //对数组中存放的元素重新排序
                NSString *imageStr = self.imagesArray[cellIndexPath.row];
                [self.imagesArray removeObjectAtIndex:cellIndexPath.row];
                [self.imagesArray insertObject:imageStr atIndex:attributes.indexPath.row];
                [self.mineCollection moveItemAtIndexPath:cellIndexPath toIndexPath:attributes.indexPath];
                
                
            }
        }
        
    }else if (sender.state == UIGestureRecognizerStateEnded) {
        if (!isChanged) {
            cell.center = [_mineCollection layoutAttributesForItemAtIndexPath:cellIndexPath].center;
        }
        NSLog(@"排序后---%@",self.imagesArray);
    }
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值