// // HJCycleView.m // IphoneBIMe // // Created by ZhangJF on 2022/10/28. // Copyright © 2022 ProBIM. All rights reserved. // #import "HJCycleView.h" #import "CycleCollectionViewCell.h" #define kSelfWidth self.frame.size.width #define kSelfHeight self.frame.size.height static NSString *const ImageCellID = @"ImageCellID"; @interface HJCycleView(){ NSTimer *_timer; //暂时已把定时关闭 } @property(nonatomic,strong)UICollectionView *collectionView; @property(nonatomic,strong)UIPageControl *pageControl; @property(nonatomic,strong)NSMutableArray *dataArr; @end @implementation HJCycleView -(instancetype)initWithFrame:(CGRect)frame andData:(NSArray*)dataArr{ if (self = [super initWithFrame:frame]) { self.dataArr = [NSMutableArray arrayWithArray:dataArr]; [self setUI]; } return self; } - (void)setUI{ [self addSubview:self.collectionView]; [self addSubview:self.pageControl]; } #pragma mark delegate && dataSource - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ CycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellID forIndexPath:indexPath]; cell.imageUrl = [_dataArr objectAtIndex:indexPath.item]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.returnBlock) { self.returnBlock(indexPath.row); } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new]; photoBrowser.delegate = self; photoBrowser.currentImageIndex = indexPath.item; photoBrowser.imageCount = _dataArr.count; // photoBrowser.sourceImagesContainerView = self.collectionView; [photoBrowser show]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _dataArr.count; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ // [_timer setFireDate:[NSDate distantFuture]]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSInteger page = scrollView.contentOffset.x / kSelfWidth; _pageControl.currentPage = page; } #pragma mark getter - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(kSelfWidth, kSelfHeight); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = 0; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kSelfWidth, kSelfHeight) collectionViewLayout:layout]; [_collectionView registerClass:[CycleCollectionViewCell class] forCellWithReuseIdentifier:ImageCellID]; _collectionView.bounces = YES; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.pagingEnabled = YES; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor whiteColor]; } return _collectionView; } - (UIPageControl *)pageControl{ if (!_pageControl) { _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kSelfHeight - 30, kSelfWidth, 10)]; _pageControl.currentPage = 0; _pageControl.numberOfPages = self.dataArr.count; _pageControl.backgroundColor = [UIColor clearColor]; _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:122 blue:255 alpha:1]; } return _pageControl; } - (NSMutableArray *)dataArr{ if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (void)setData:(NSArray *)data { _data = data; self.dataArr = [NSMutableArray arrayWithArray:data]; _pageControl.currentPage = 1; _pageControl.numberOfPages = self.dataArr.count; [_collectionView reloadData]; } -(void)dealloc{ // if (_timer) { // [_timer invalidate]; // _timer = nil; // } } #pragma mark - SDPhotoBrowserDelegate - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { // 不建议用此种方式获取小图,这里只是为了简单实现展示而已 CycleCollectionViewCell *cell = (CycleCollectionViewCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; return cell.imageV.image; } @end