BMACollectionViewLayouts 1.0.0

BMACollectionViewLayouts 1.0.0

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2015年3月

Miguel Angel Quinones 维护。



  • 作者:
  • Miguel Angel Quinones

一个用于满足所有布局需求的 UICollectionView 子类集。

安装

您可以使用 cocoapods 导入 BMACollectionViewLayouts

pod 'BMACollectionViewLayouts'

您还可以将此存储库作为子模块导入,并使用 'Source' 子目录下的类。

布局

目前只有一个布局,但还有更多即将到来 :rocket:

BMAReorderableFlowLayout

这是一个流布局的子类,允许用户在集合视图中拖放元素。

布局会在整个交互过程中提供所有事件的回调,并允许您控制是否允许单个或所有单元格进行整个排序。

/// Return YES if the item can be dragged. Default assumed NO.
- (BOOL)collectionView:(UICollectionView *)collectionView canDragItemAtIndexPath:(NSIndexPath *)indexPath;

/// Return YES if the item can be moved from the position to the position. Default assumed NO.
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemFromIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)toIndexPath;

/// Reported if item can be moved, after animation happened
- (void)collectionView:(UICollectionView *)collectionView didMoveItemFromIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)toIndexPath;

/// Notifies that a cell has been selected and will begin dragging it
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

/// Notifies that a cell has been selected and did begin dragging it. Called after animations before dragging
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

/// Notifies that a cell previously being dragged will end dragging
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

/// Notifies that a cell previously dragged has been dropped to it's destination. Called after all animations.
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

一个有用的功能是可以挂钩正在拖拽的实际单元格的动画。通常需要从视觉上表示集合视图允许用户拖拽单元格,但设计应取决于您。因此,您可以为这种效果实现两个回调

/// Gives possibility to customise the animation of the cell when it's selected for dragging
- (BMAReorderingAnimationBlock)animationForDragBeganInCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout;

/// Gives possibility to customise the animation of the cell when it's dropped after dragging
- (BMAReorderingAnimationBlock)animationForDragEndedInCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout;

一个简单的弹出动画示例

- (BMAReorderingAnimationBlock)animationForDragBeganInCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout {
    return ^(UICollectionViewCell *draggedView){
        draggedView.transform = CGAffineTransformMakeScale(1.3, 1.3);
    };
}

- (BMAReorderingAnimationBlock)animationForDragEndedInCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout {
    return ^(UICollectionViewCell *draggedView){
        draggedView.transform = CGAffineTransformIdentity;
    };
}

以及它看起来像什么

image

重要

布局需要复制单元格以便创建可以移动的视图。它这样做而不是创建快照,因为您可能希望在单元格实际移动之前重新配置单元格。因此,您需要在您的单元格中实现 NSCopying 并返回一个有效的副本。