zjf
2023-03-13 881f0da670f20c401c1e1d08b36253abb28f72d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//  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()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate, SDPhotoBrowserDelegate>{
    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