频道界面的显示 ------------》点击进行添加/删除--------》 按住可以进行拖拽排序
- -(void)buildUI
- {
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
- CGFloat cellWidth = (self.view.bounds.size.width - (ColumnNumber + 1) * CellMarginX)/ColumnNumber;
- flowLayout.itemSize = CGSizeMake(cellWidth,cellWidth);
- flowLayout.sectionInset = UIEdgeInsetsMake(0, CellMarginX, CellMarginY, CellMarginX);
- flowLayout.minimumLineSpacing = CellMarginY;
- flowLayout.minimumInteritemSpacing = CellMarginX;
- flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
- _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
- _collectionView.showsHorizontalScrollIndicator = false;
- _collectionView.backgroundColor = [UIColor clearColor];
- [_collectionView registerClass:[TestCell class] forCellWithReuseIdentifier:@"TestCell"];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- [self.view addSubview:_collectionView];
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMethod:)];
- longPress.minimumPressDuration = 0.3f;
- [_collectionView addGestureRecognizer:longPress];
- _dragingCell = [[TestCell alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellWidth/2.0f)];
- _dragingCell.hidden = true;
- [_collectionView addSubview:_dragingCell];
- }
- -(void)longPressMethod:(UILongPressGestureRecognizer*)gesture
- {
- switch (gesture.state) {
- case UIGestureRecognizerStateBegan:
- [self dragBegin:gesture];
- break;
- case UIGestureRecognizerStateChanged:
- [self dragChanged:gesture];
- break;
- case UIGestureRecognizerStateEnded:
- [self dragEnd:gesture];
- break;
- default:
- break;
- }
- }
- -(void)dragBegin:(UILongPressGestureRecognizer*)gesture{
- }
- -(void)dragChanged:(UILongPressGestureRecognizer*)gesture{
- }
- -(void)dragEnd:(UILongPressGestureRecognizer*)gesture{
- }
- //获取被拖动IndexPath的方法
- -(NSIndexPath*)getDragingIndexPathWithPoint:(CGPoint)point
- {
- NSIndexPath* dragingIndexPath = nil;
- //遍历所有屏幕上的cell
- for (NSIndexPath *indexPath in [_collectionView indexPathsForVisibleItems]) {
- //判断cell是否包含这个点
- if (CGRectContainsPoint([_collectionView cellForItemAtIndexPath:indexPath].frame, point)) {
- dragingIndexPath = indexPath;
- break;
- }
- }
- return dragingIndexPath;
- }
- -(void)dragBegin:(UILongPressGestureRecognizer*)gesture{
- CGPoint point = [gesture locationInView:_collectionView];
- _dragingIndexPath = [self getDragingIndexPathWithPoint:point];
- if (!_dragingIndexPath) {return;}
- NSLog(@"拖拽开始 indexPath = %@",_dragingIndexPath);
- [_collectionView bringSubviewToFront:_dragingCell];
- //更新被拖拽的cell
- _dragingCell.frame = [_collectionView cellForItemAtIndexPath:_dragingIndexPath].frame;
- _dragingCell.hidden = false;
- [UIView animateWithDuration:0.3 animations:^{
- [_dragingCell setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
- }];
- }
- //正在拖拽的indexpath
- NSIndexPath *_dragingIndexPath;
- //目标位置
- NSIndexPath *_targetIndexPath;
- //获取目标IndexPath的方法
- -(NSIndexPath*)getTargetIndexPathWithPoint:(CGPoint)point
- {
- NSIndexPath *targetIndexPath = nil;
- //遍历所有屏幕上的cell
- for (NSIndexPath *indexPath in _collectionView.indexPathsForVisibleItems) {
- //避免和当前拖拽的cell重复
- if ([indexPath isEqual:_dragingIndexPath]) {continue;}
- //判断是否包含这个点
- if (CGRectContainsPoint([_collectionView cellForItemAtIndexPath:indexPath].frame, point)) {
- targetIndexPath = indexPath;
- }
- }
- return targetIndexPath;
- }
拖拽_dragingCell并交换位置的方法如下:- -(void)dragChanged:(UILongPressGestureRecognizer*)gesture{
- NSLog(@"拖拽中。。。");
- CGPoint point = [gesture locationInView:_collectionView];
- _dragingCell.center = point;
- _targetIndexPath = [self getTargetIndexPathWithPoint:point];
- NSLog(@"targetIndexPath = %@",_targetIndexPath);
- if (_targetIndexPath && _dragingIndexPath) {
- [_collectionView moveItemAtIndexPath:_dragingIndexPath toIndexPath:_targetIndexPath];
- _dragingIndexPath = _targetIndexPath;
- }
- }
效果如下:- -(void)dragEnd:(UILongPressGestureRecognizer*)gesture{
- NSLog(@"拖拽结束");
- if (!_dragingIndexPath) {return;}
- CGRect endFrame = [_collectionView cellForItemAtIndexPath:_dragingIndexPath].frame;
- [UIView animateWithDuration:0.3 animations:^{
- [_dragingCell setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
- _dragingCell.frame = endFrame;
- }completion:^(BOOL finished) {
- _dragingCell.hidden = true;
- }];
- }
联系客服