动画设计学习iOS 开发每天分享优质文章程序员

UICollectionView自定义相册

2016-10-07  本文已影响501人  IIronMan

效果图

Untitled11卡片动画1.gif

一.我们要对UICollectionView的布局进行自定义

这是我自定义的宏以及静态变量

#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
#define CWColor(a,b,c) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:1.0]
/**
 *   static 表示只在此文件里可以访问(防止其他文件访问) const防止别人去改
 */
static NSString *const ID = @"cellID";
  /**
   *   UICollectionView的创建
   */
   UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, WIDTH) collectionViewLayout:layout];
   collectionView.backgroundColor = CWColor(255, 255, 0);
   collectionView.showsHorizontalScrollIndicator = NO;

   collectionView.delegate = self;
   collectionView.dataSource = self;
   /**
    *  注册UICollectionView
    */
  [collectionView registerNib:[UINib nibWithNibName:@"CWImageCell" bundle:nil] forCellWithReuseIdentifier:ID];
  [self.view addSubview: collectionView];

3.CollectionView的代理方法

 #pragma mark  collectionView的代理方法的实现
-(NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

CWImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];

cell.imageName = self.imageArray[indexPath.item];

cell.number.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item+1];

return cell;

}

/**
 *  collectionView的点击事件
 */
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //删除模型数据(删除的指定item)
    [self.imageArray removeObjectAtIndex:indexPath.item];

    //刷新UI
    [collectionView deleteItemsAtIndexPaths:@[indexPath]];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/**
 *  动画式切换
 */
if ([self.collectionView.collectionViewLayout isKindOfClass:[CwLineLayout class]]) {
    
    [self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc]init] animated:YES];
  }else
  {
       [self.collectionView setCollectionViewLayout:[[CwLineLayout alloc]init] animated:YES];
  }
}
#pragma mark 图片数组化
-(NSMutableArray *)imageArray
{
   if (!_imageArray) {
    
    _imageArray = [[NSMutableArray alloc]init];
    
    for (int i = 1; i <= 13 ; i++) {
        
        [_imageArray addObject:[NSString stringWithFormat:@"%d.jpg",i]];
    }
 }
  return _imageArray;
}

简单的相册demo 密码: fm69

实例2旋转相册 密码:hwh3

旋转相册

值得注意的是layout的一个属性

// zIndex越大,就越在上面
attrs.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item;

实例3.圆形角度相册 密码: yj7w

圆形角度相册.gif

大总结:在进行layout 进行布局时记住如下几点:

提示: UICollectionView 滚动条去掉

  垂直滚动条
  XXX.showsVerticalScrollIndicator = NO;
  水平滚动条
  XXX.showsHorizontalScrollIndicator = NO;
上一篇 下一篇

猜你喜欢

热点阅读