样式
样式
你的VC 中哈 我放在view里了,你直接放在你的 VC里就好。
// 轮播item
#import "BaseBannerItemView.h"
#import "BaseBannerItemFlowLayout.h"
#import "BaseBannerItemCell.h"
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface BaseBannerItemView ()
<
UICollectionViewDelegate, UICollectionViewDataSource
>
@property (nonatomic, strong) UICollectionView *collectionView; ///< 日历视图
@property (nonatomic, strong) NSMutableArray *dateArray; ///< 数据源数组
@property (nonatomic, strong) UIPageControl *pageControl; ///<
@end
@implementation BaseBannerItemView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addContUI];
}
return self;
}
// MARK: ----- UI
- (void)addContUI
{
self.dateArray = [@[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13] mutableCopy];
NSInteger indexCount = 1;
if (self.dateArray.count % (10) != 0)
{
indexCount = self.dateArray.count / (10) + 1;
}
else
{
indexCount = self.dateArray.count / (10);
}
self.pageControl.numberOfPages = indexCount;
//
[self addSubview:self.pageControl];
[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(0);
make.height.mas_equalTo(RATIOA(15));
}];
//
[self addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.mas_equalTo(0);
make.bottom.mas_equalTo(self.pageControl.mas_top);
}];
}
- (void)pageControlValueChange:(UIPageControl *)pageControl
{
NSLog(@"%ld", pageControl.currentPage); // 根据当前页的不同的值, 可以实现不同的功能
self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width * pageControl.currentPage, 0);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.collectionView.frame.size.width;
self.pageControl.currentPage = self.collectionView.contentOffset.x / pageWidth;
}
// MARK: ----- 数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// if (section == 0) {
// return 9;
// }
// else if (section == 1) {
// return 17;
// }
// else if (section == 2) {
// return 25;
// }
return self.dateArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
// cell.backgroundColor = [UIColor redColor];
// UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
// label.backgroundColor = [UIColor redColor];
// label.textAlignment = NSTextAlignmentCenter;
// label.font = [UIFont systemFontOfSize:15];
// label.text = [NSString stringWithFormat:@"%ld", indexPath.row];
// label.textColor = [UIColor blueColor];
// label.numberOfLines = 0;
// [cell.contentView addSubview:label];
// return cell;
BaseBannerItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemCell" forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section %ld row --- %ld", indexPath.section, indexPath.row);
}
// MARK: ----- lazy
// 日历视图
- (UICollectionView *)collectionView
{
if (!_collectionView) {
// 横向滚动
BaseBannerItemFlowLayout *layout = [[BaseBannerItemFlowLayout alloc] init];
layout.itemSize = CGSizeMake(kScreenWidth/5, RATIOA(150/2));
//_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, kScreenWidth) collectionViewLayout:horizontalLayout];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_collectionView.pagingEnabled = YES;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.bounces = NO;
_collectionView.pagingEnabled = YES;
_collectionView.backgroundColor = [UIColor blueColor];
//[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[_collectionView registerClass:[BaseBannerItemCell class] forCellWithReuseIdentifier:@"itemCell"];
}
return _collectionView;
}
// 数据源数组
- (NSMutableArray *)dateArray
{
if (!_dateArray) {
_dateArray = [NSMutableArray array];
}
return _dateArray;
}
- (UIPageControl *)pageControl
{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] init];
_pageControl.backgroundColor = [UIColor orangeColor];
// 设置总页数
//_pageControl.numberOfPages = indexCount;
_pageControl.pageIndicatorTintColor = [UIColor cyanColor];
// 设置当前所在页数
_pageControl.currentPage = 0;
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[_pageControl addTarget:self action:@selector(pageControlValueChange:) forControlEvents:UIControlEventValueChanged];
// 单页时隐藏
//_pageControl.hidesForSinglePage = YES;
}
return _pageControl;
}
@end
这里请注意 每页份额
self.dateArray = [@[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13] mutableCopy];
NSInteger indexCount = 1;
if (self.dateArray.count % (10) != 0)
{
indexCount = self.dateArray.count / (10) + 1;
}
else
{
indexCount = self.dateArray.count / (10);
}
self.pageControl.numberOfPages = indexCount;
自定义 重要
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BaseBannerItemFlowLayout : UICollectionViewFlowLayout
@end
NS_ASSUME_NONNULL_END
//
#import "BaseBannerItemFlowLayout.h"
@interface BaseBannerItemFlowLayout ()
@property (nonatomic, copy) NSMutableDictionary *sectionDic;
@property (nonatomic, strong) NSMutableArray *allAttributes;
@end
@implementation BaseBannerItemFlowLayout
- (instancetype)init
{
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
- (void)prepareLayout
{
[super prepareLayout];
_sectionDic = [NSMutableDictionary dictionary];
self.allAttributes = [NSMutableArray array];
//获取section的数量
NSUInteger section = [self.collectionView numberOfSections];
for (int sec = 0; sec < section; sec++)
{
//获取每个section的cell个数
NSUInteger count = [self.collectionView numberOfItemsInSection:sec];
for (NSUInteger item = 0; item<count; item++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:sec];
//重新排列
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.allAttributes addObject:attributes];
}
}
}
- (CGSize)collectionViewContentSize
{
//每个section的页码的总数
NSInteger actualLo = 0;
for (NSString *key in [_sectionDic allKeys])
{
actualLo += [_sectionDic[key] integerValue];
}
return CGSizeMake(actualLo*self.collectionView.frame.size.width, self.collectionView.contentSize.height);
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
{
if (attributes.representedElementKind != nil)
{
return;
}
/*修改by lixinkai 2017.6.30
下面这两个方法 itemW、itemH
解决了同一个UICollectionView使用不同UICollectionViewCell的问题
*/
// attributes 的宽度
CGFloat itemW = attributes.frame.size.width;
// attributes 的高度
CGFloat itemH = attributes.frame.size.height;
// collectionView 的宽度
CGFloat width = self.collectionView.frame.size.width;
// collectionView 的高度
CGFloat height = self.collectionView.frame.size.height;
// 每个attributes的下标值 从0开始
NSInteger itemIndex = attributes.indexPath.item;
CGFloat stride = (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) ? width : height;
// 获取现在的attributes是第几组
NSInteger section = attributes.indexPath.section;
// 获取每个section的item的个数
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
CGFloat offset = section * stride;
// 计算x方向item个数
NSInteger xCount = (width / itemW);
// 计算y方向item个数
NSInteger yCount = (height / itemH);
// 计算一页总个数
NSInteger allCount = (xCount * yCount);
// 获取每个section的页数,从0开始
NSInteger page = itemIndex / allCount;
// 余数,用来计算item的x的偏移量
NSInteger remain = (itemIndex % xCount);
// 取商,用来计算item的y的偏移量
NSInteger merchant = (itemIndex-page*allCount)/xCount;
// x方向每个item的偏移量
CGFloat xCellOffset = remain * itemW;
// y方向每个item的偏移量
CGFloat yCellOffset = merchant * itemH;
// 获取每个section中item占了几页
NSInteger pageRe = (itemCount % allCount == 0)? (itemCount / allCount) : (itemCount / allCount) + 1;
// 将每个section与pageRe对应,计算下面的位置
[_sectionDic setValue:@(pageRe) forKey:[NSString stringWithFormat:@"%ld", section]];
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal)
{
NSInteger actualLo = 0;
// 将每个section中的页数相加
for (NSString *key in [_sectionDic allKeys])
{
actualLo += [_sectionDic[key] integerValue];
}
// 获取到的最后的数减去最后一组的页码数
actualLo -= [_sectionDic[[NSString stringWithFormat:@"%ld", [_sectionDic allKeys].count-1]] integerValue];
xCellOffset += page*width + actualLo*width;
}
else
{
yCellOffset += offset;
}
attributes.frame = CGRectMake(xCellOffset, yCellOffset, itemW, itemH);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
//UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
UICollectionViewLayoutAttributes *attr = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
[self applyLayoutAttributes:attr];
return attr;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.allAttributes;
}
@end
网友评论