#import "ViewController.h"
#define KscreenHeigh [[UIScreen mainScreen] bounds].size.height
#define KscreenWidth [[UIScreen mainScreen] bounds].size.width
// 注意const的位置
static NSString *const cellId = @"cell";
static NSString *const headerId = @"header";
static NSString *const footerId = @"footer";
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
CGFloat cellWidth;
NSMutableArray *_section0Array;
}
@property(nonatomic,strong)UICollectionView *collectionView;
@end
@implementation ViewController
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 self.view.backgroundColor = [UIColor cyanColor]; 4 _section0Array = [NSMutableArray arrayWithCapacity:10]; 5 _section0Array =[NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4"]]; 6 cellWidth = KscreenWidth/4-10; 7 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; 8 _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, KscreenWidth, KscreenHeigh-64) collectionViewLayout:layout]; 9 _collectionView.delegate = self; 10 _collectionView.dataSource = self; 11 [self.view addSubview:_collectionView]; 12 _collectionView.backgroundColor = [UIColor whiteColor]; 14 // 注册cell、sectionHeader、sectionFooter 15 [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId]; 16 [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId]; 17 [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId]; 18 }
实现数据源delegate
1 #pragma mark ---- UICollectionViewDataSource 2 // 设置section数量 3 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 4 { 5 return 2; 6 } 7 // 设置每个section里有多少item 8 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 9 { 10 return _section0Array.count; 11 } 12 // 设置cell ,可自定义UICollectionViewCell 13 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 14 { 15 UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; 16 cell.backgroundColor = [UIColor greenColor]; 17 return cell; 18 } 19 // 和UITableView类似,UICollectionView也可设置段头段尾 20 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 21 { 22 if([kind isEqualToString:UICollectionElementKindSectionHeader]) 23 { 24 UICollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath]; 25 if(headerView == nil) 26 { 27 headerView = [[UICollectionReusableView alloc] init]; 28 } 29 headerView.backgroundColor = [UIColor grayColor]; 30 31 return headerView; 32 } 33 else if([kind isEqualToString:UICollectionElementKindSectionFooter]) 34 { 35 UICollectionReusableView *footerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath]; 36 if(footerView == nil) 37 { 38 footerView = [[UICollectionReusableView alloc] init]; 39 } 40 footerView.backgroundColor = [UIColor lightGrayColor]; 41 42 return footerView; 43 } 44 return nil; 45 } 46 //允许移动item 47 - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath 48 { 49 return YES; 50 } 51 52 //移动item从一个indexPath到另一个indexPath 53 - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath 54 { 55 56 }
实现布局的delegate
1 #pragma mark ---- UICollectionViewDelegateFlowLayout 2 //设置item 大小 3 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 4 { 5 return (CGSize){cellWidth,cellWidth}; 6 } 7 8 //每个分组的边缘尺寸 9 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 10 { 11 return UIEdgeInsetsMake(5, 5, 5, 5); 12 } 13 //每个分组的/行间距 14 15 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section 16 { 17 return 5.f; 18 } 19 20 //每个分组的/列间距 21 22 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section 23 { 24 return 25.f; 25 } 26 //每个分组的头部尺寸 27 28 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 29 { 30 return (CGSize){KscreenWidth,44}; 31 } 32 33 //每个分组的底部尺寸 34 35 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section 36 { 37 return (CGSize){KscreenWidth,22}; 38 }
其他不太常用的delegate
1 #pragma mark ---- UICollectionViewDelegate 2 3 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath 4 { 5 return YES; 6 } 7 8 // 点击高亮 9 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath 10 { 11 UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 12 cell.backgroundColor = [UIColor greenColor]; 13 } 14 15 16 // 选中某item 17 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 18 { 19 20 } 21 22 23 // 长按某item,弹出copy和paste的菜单 24 - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath 25 { 26 return YES; 27 } 28 29 // 使copy和paste有效 30 - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender 31 { 32 if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"]) 33 { 34 return YES; 35 } 36 37 return NO; 38 } 39 40 41 - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender 42 { 43 if([NSStringFromSelector(action) isEqualToString:@"copy:"]) 44 { 45 NSLog(@"-------------执行拷贝-------------"); 46 [_collectionView performBatchUpdates:^{ 47 [_section0Array removeObjectAtIndex:indexPath.row]; 48 [_collectionView deleteItemsAtIndexPaths:@[indexPath]]; 49 } completion:nil]; 50 } 51 else if([NSStringFromSelector(action) isEqualToString:@"paste:"]) 52 { 53 NSLog(@"-------------执行粘贴-------------"); 54 } 55 }
附加长按移动的手势实现方法
//添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.collectionView addGestureRecognizer:longPress];
1 - (void)longPress:(UILongPressGestureRecognizer *)longPress{ 2 UIGestureRecognizerState state = longPress.state; 3 switch (state) { 4 case UIGestureRecognizerStateBegan:{ 5 CGPoint pressPoint = [longPress locationInView:self.collectionView]; 6 NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pressPoint]; 7 if (!indexPath) { 8 break; 9 } 10 [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; 11 break; 12 } 13 case UIGestureRecognizerStateChanged:{ 14 CGPoint pressPoint = [longPress locationInView:self.collectionView]; 15 [self.collectionView updateInteractiveMovementTargetPosition:pressPoint]; 16 break; 17 } 18 case UIGestureRecognizerStateEnded:{ 19 [self.collectionView endInteractiveMovement]; 20 break; 21 } 22 default: 23 [self.collectionView cancelInteractiveMovement]; 24 break; 25 } 26 }
完毕