这次的电影项目,很多地方都用到了collectionView,像图片的显示,相册的显示等等。这里我要说的是图片的联动,item的位置计算,蒙版的添加。强调下item的计算需要关闭自动分页。
1、collectionView的创建,minimumLineSpacing这个属性如果是垂直cell的话,那么cell的之间的行间距用这个,如果是水平cell的话,那么列间距用这个。数据的显示都是套路,创建模型,自定义cell,设置模型属性,从控制器注册cell并传值。
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *posterView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WScreen, self.height) collectionViewLayout:layout];
self.posterView = posterView;
layout.itemSize = CGSizeMake(WScreen-100, self.height-100);
layout.minimumLineSpacing = 50;
// 水平滑动
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
posterView.backgroundColor = [UIColor orangeColor];
posterView.hidden = NO;
// 分页效果,因为自动计算,所有关闭
// posterView.pagingEnabled = YES;
posterView.dataSource = self;
posterView.delegate = self;
// 水平挪动
posterView.showsHorizontalScrollIndicator = NO;
[self addSubview:posterView];
// 注册
[posterView registerClass:[WXPosterCell class] forCellWithReuseIdentifier:@"cell"];
2、item的计算,用到的就是这个方法,主要是根据偏移量计算第几页
// 做这个功能一定要关闭分页效果
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
// 1.根据偏移量来计算第几个item
CGFloat offSetX = targetContentOffset->x;
// 2.item的宽度
CGFloat itemWidth = WScreen - 100;
// 3.页码的宽度
NSInteger pageWith = itemWidth + 50;
// 4.根据偏移量计算第几页
NSInteger pageNum = (offSetX + pageWith/2) / pageWith;
// 5.根据第几个item改变偏移量
targetContentOffset->x = pageWith * pageNum;
self.smallView.posterIndex = pageNum;
}
3、联动效果是使用观察者实现
#pragma mark - 观察者
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
NSInteger index = [[change objectForKey:@"new"]integerValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[UIView animateWithDuration:0.35 animations:^{
[self.posterView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}];
}
4、下拉菜单的实现
// 创建按钮
UIButton *button = [[UIButton alloc] init];
self.btn = button;
button.frame = CGRectMake((self.headView.width - 20)/2, self.headView.height - 20, 20, 20);
[button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventTouchUpInside];
[self.headView addSubview:button];
#pragma mark - 显示或隐藏
- (void)shouHead {
[UIView animateWithDuration:0.35 animations:^{
self.headView.transform = CGAffineTransformMakeTranslation(0, self.headView.height-42);
self.hud.hidden = NO;
}];
}
- (void)hiddenHead {
[UIView animateWithDuration:0.35 animations:^{
self.headView.transform = CGAffineTransformIdentity;
self.hud.hidden = YES;
}];
}
- (void)selectedAction:(UIButton *)btn {
if (!btn.selected) {
[self shouHead];
} else {
[self hiddenHead];
}
btn.selected = !btn.selected;
}