一.创建海报视图 :
- (void)_createPosterView{ // _posterView = [[UIView alloc] initWithFrame:self.view.bounds]; // _posterView.backgroundColor = [UIColor orangeColor]; // _posterView.hidden = NO; // // [self.view addSubview:_posterView]; //布局对象 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //布局信息 flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavigationBarHeight - kTabBarHeight); //flowLayout.minimumInteritemSpacing = 0; flowLayout.minimumLineSpacing = 30; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _posterView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight - kTabBarHeight) collectionViewLayout:flowLayout]; _posterView.dataSource = self; _posterView.delegate = self; //分页效果 _posterView.pagingEnabled = YES; _posterView.indicatorStyle = UIScrollViewIndicatorStyleWhite; [self.view addSubview:_posterView]; //注册单元格 [_posterView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionView_Cell"]; [self.view addSubview:_posterView]; }
二.代码优化:
子类化PostView:
// // PosterView.m // Movie 2.0 // // Created by mac1 on 15/10/11. // Copyright (c) 2015年 www.iphonetrain.com. All rights reserved. // #import "PosterView.h" #import "LargeCell.h" #import "Common.h" #import "MovieModel.h" #import "UIImageView+WebCache.h" @implementation PosterView #define kLargeCollectionViewCellID @"kLargeCollectionViewCellID" #define kMovieHeaderViewHeight 50 #define kMovieFooterViewHeight 40 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self != nil) { [self _createLargeCollectionView]; } return self; } - (void)_createLargeCollectionView{ //布局对象 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //布局信息 flowLayout.itemSize = CGSizeMake(kScreenWidth * 0.75, kScreenHeight - kNavigationBarHeight - kTabBarHeight - kMovieHeaderViewHeight - kMovieFooterViewHeight); //上下分别留出来其他两个部分的位置, //减去距离CollectionView顶部和底部空余的空间 // flowLayout.minimumInteritemSpacing = 0; flowLayout.minimumLineSpacing = 0; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; CGRect largeFrame = CGRectMake( 0, kMovieHeaderViewHeight, kScreenWidth, kScreenHeight - kNavigationBarHeight - kTabBarHeight - kMovieHeaderViewHeight - kMovieFooterViewHeight); /*上下分别留出来其他两个部分的位置*/ _largeCollectionView = [[UICollectionView alloc] initWithFrame:largeFrame collectionViewLayout:flowLayout]; _largeCollectionView.dataSource = self; _largeCollectionView.delegate = self; //分页效果 _largeCollectionView.pagingEnabled = YES; _largeCollectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite; [self addSubview:_largeCollectionView]; //注册单元格 [_largeCollectionView registerClass:[LargeCell class] forCellWithReuseIdentifier:kLargeCollectionViewCellID]; } #pragma mark - CollectionView数据源代理 //组数- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1;} //单元格的个数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.data.count;} //创建单元格- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { LargeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLargeCollectionViewCellID forIndexPath:indexPath]; // cell.transform = CGAffineTransformMakeScale(0.5, 0.5); //电影数据传递给单元格显示 MovieModel *model = self.data[indexPath.item]; cell.model = model; // //取到图片URL地址 // MovieModel *model = self.data[indexPath.item]; // NSString *imageStr = [model.images objectForKey:@"large"]; // NSURL *imageUrl = [NSURL URLWithString:imageStr]; // // //创建ImageView显示图片 // UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; // [imageView sd_setImageWithURL:imageUrl]; // // //imageView添加到单元格上 // [cell.contentView addSubview:imageView]; return cell;} //设置每一组的偏移量(上左下右四个方向)- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, (kScreenWidth - kScreenWidth * 0.75) / 2, 0, 0); //(屏幕宽度 - 单元格宽度) / 2} @end
控制器调用创建PostView :
//创建海报视图 - (void)_createPosterView{ // _posterView = [[UIView alloc] initWithFrame:self.view.bounds]; // _posterView.backgroundColor = [UIColor orangeColor]; // _posterView.hidden = NO; // // [self.view addSubview:_posterView]; _posterView = [[PosterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight - kTabBarHeight)]; _posterView.backgroundColor = [UIColor orangeColor]; _posterView.hidden = NO; //把数据从控制器传递给海报视图 _posterView.data = self.movieData; [self.view addSubview:_posterView]; }
三.给PostView赋数据,子类化collectionCell:
// // LargeCell.h // Movie 2.0 // // Created by mac1 on 15/10/11. // Copyright (c) 2015年 www.iphonetrain.com. All rights reserved. // #import <UIKit/UIKit.h> //大海报CollectionView单元格 @class MovieModel; @interface LargeCell : UICollectionViewCell { UIImageView *_imageView; //电影海报 } @property (nonatomic, strong) MovieModel *model; //一条电影数据 @end
// // LargeCell.m // Movie 2.0 // // Created by mac1 on 15/10/11. // Copyright (c) 2015年 www.iphonetrain.com. All rights reserved. // #import "LargeCell.h" #import "UIViewExt.h" #import "UIImageView+WebCache.h" #import "MovieModel.h" @implementation LargeCell /* 当单元格是从xib文件加载,此方法会被调用 - (void)awakeFromNib{ } */ //创建单元格调用此方法: - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //创建子视图 [self _createSubviews]; } return self; } - (void)_createSubviews { //单元格的宽高 * 0.8 = 图片的大小 CGFloat width = self.width * 0.8; CGFloat height = self.height * 0.8; //创建imageView _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; _imageView.center = self.contentView.center; [self.contentView addSubview:_imageView]; } //给子视图填充数据 - (void)setModel:(MovieModel *)model{ _model = model; NSString *imageStr = [model.images objectForKey:@"large"]; NSURL *imageURL = [NSURL URLWithString:imageStr]; [_imageView sd_setImageWithURL:imageURL]; } @end
实现的效果如下 :
时间: 2024-10-13 08:55:02