// // HDLCoverFlow.m // CoverFlow // // Created by JLCHEN on 2019/8/23. // Copyright © 2019 JLCHEN. All rights reserved. // #import "HDLCoverFlow.h" #import "HDLCoverFlowCell.h" #import "HDLCoverFlowLayout.h" @interface HDLCoverFlow() @property (nonatomic,strong) UICollectionView *mUICollectionView; @end @implementation HDLCoverFlow{ NSArray *viewDataArray; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self addSubview:self.mUICollectionView]; } return self; } #pragma mark- 子控件坐标 //当控件本身的尺寸发送改变时,系统会自动调用这个方法 - (void)layoutSubviews{ [super layoutSubviews]; CGFloat personW = self.frame.size.width; CGFloat personH = self.frame.size.height; self.mUICollectionView.frame = CGRectMake(0, 0, personW, personH); } #pragma mark- 懒加载 - (UICollectionView *)mUICollectionView{ if (_mUICollectionView == nil) { HDLCoverFlowLayout *flowLayout = [[HDLCoverFlowLayout alloc]init]; _mUICollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout]; _mUICollectionView.backgroundColor = [UIColor lightGrayColor]; // 设置数据源对象 _mUICollectionView.dataSource = self; _mUICollectionView.delegate = self; // 注册cell [_mUICollectionView registerClass:[HDLCoverFlowCell class] forCellWithReuseIdentifier:@"CoverFlowID"]; } return _mUICollectionView; } #pragma mark - 数据源方法 // 返回item的个数! - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return viewDataArray.count; } // 返回cell! - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell HDLCoverFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CoverFlowID" forIndexPath:indexPath]; [cell setCustomViewCell: viewDataArray[indexPath.row]]; // 3.返回cell return cell; } #pragma mark --UICollectionViewDelegate //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { // // 1.创建cell // ZHCoverFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CoverFlowID" forIndexPath:indexPath]; // cell.backgroundColor = [UIColor redColor]; NSLog(@"点击了:%ld",(indexPath.row)); [_mSelectItemDelegate onSelectItem:indexPath.row]; } #pragma mark - 加载数据 -(void)addCoverFlowViewDatas:(NSArray *) viewsData{ viewDataArray = viewsData; [_mUICollectionView reloadData]; } /** 设置选中 @param selectIndex */ -(void)setCoverFlowSelectIndex:(NSInteger) selectIndex{ [self.mUICollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; // _mUICollectionView } /** 重置布局 @param mCGRect 布局 */ -(void)initWithFrameArc:(CGRect) mCGRect{ self.frame = mCGRect; _mUICollectionView.frame = self.frame; _mUICollectionView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); } @end