forked from WellsYC/YCPhotoBrower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YCPhotoBrowserController.m
212 lines (179 loc) · 7.91 KB
/
YCPhotoBrowserController.m
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// YCPhotoBrowserController.m
// YCToolkit
//
// Created by 蔡亚超 on 2018/1/31.
// Copyright © 2018年 WellsCai. All rights reserved.
//
#import "YCPhotoBrowserController.h"
#import "YCPhotoBrowserCell.h"
#import "YCPhotoBrowserConst.h"
#import "YCPhotoBrowserCellHelper.h"
#import "YCPhotoBrowserAnimator.h"
#import "UIView+YCExtension.h"
@interface YCPhotoBrowserController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate,YCPhotoBrowserCellDelegate,AnimatorDismissedDelegate>
{
long _imagesCount;
YCPhotoSourceType _photoSourceType;
}
@property(nonatomic,strong)NSArray *showImagesOrURLs;
@property(nonatomic,strong)NSDictionary *relaceParameter;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)UILabel *indicatorLabel;
@end
@implementation YCPhotoBrowserController
static NSString *const PhotoBrowserCellID = @"PhotoBrowserCell";
#pragma mark - 初始化
+ (instancetype)instanceWithShowImages:(NSArray<UIImage *> *)showImages{
NSAssert(showImages, @"showImages can‘t be nil!");
[showImages enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSAssert([obj isKindOfClass:[UIImage class]], @"showImages must be UIImage!");
}];
return [[YCPhotoBrowserController alloc] initWithPhotoSourceType:YCPhotoSourceType_Image ShowImagesOrURLs:showImages urlReplacing:nil];
}
+ (instancetype)instanceWithShowImagesURLs:(NSArray<NSURL *> *)showImagesURLs urlReplacing:(NSDictionary *)parameter{
NSAssert(showImagesURLs, @"showImagesURLs can‘t be nil!");
[showImagesURLs enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSAssert([obj isKindOfClass:[NSString class]]||[obj isKindOfClass:[NSURL class]], @"showImagesURLs must be NSString or NSURL!");
}];
YCPhotoSourceType sourceType = parameter ?YCPhotoSourceType_AlterURL:YCPhotoSourceType_URL;
return [[YCPhotoBrowserController alloc] initWithPhotoSourceType:sourceType ShowImagesOrURLs:showImagesURLs urlReplacing:parameter];
}
- (instancetype)initWithPhotoSourceType:(YCPhotoSourceType )sourceType ShowImagesOrURLs:(NSArray *)showImagesOrURLs urlReplacing:(NSDictionary *)parameter{
if (self = [super init]) {
_showImagesOrURLs = [showImagesOrURLs copy];
_relaceParameter = [parameter copy];
_photoSourceType = sourceType;
_indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
_imagesCount = _showImagesOrURLs.count;
_isShowSaveButton = NO;
_indicatorType = YCIndicatorType_dot;
_indicatorCenter = CGPointMake(ScreenW * 0.5, ScreenH - 50);
}
return self;
}
- (void)loadView{
[super loadView];
self.view.width += 20;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.collectionView];
[self.view addSubview:self.pageControl];
[self.view addSubview:self.indicatorLabel];
[self.collectionView registerClass:YCPhotoBrowserCell.class forCellWithReuseIdentifier:PhotoBrowserCellID];
[self.collectionView scrollToItemAtIndexPath:_indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
if (_imagesCount >= 0) {
if (_indicatorType == YCIndicatorType_dot) {
self.pageControl.hidden = NO;
}else{
self.indicatorLabel.hidden = NO;
}
}
}
#pragma mark - UICollectionView数据源和代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _imagesCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YCPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoBrowserCellID forIndexPath:indexPath];
cell.delegate = self;
cell.isShowSaveButton = self.isShowSaveButton;
// Cell适配器
YCPhotoBrowserCellHelper *helper = [YCPhotoBrowserCellHelper helperWithPhotoSourceType:_photoSourceType imagesOrURL:self.showImagesOrURLs[indexPath.row] urlReplacing:self.relaceParameter];
[helper setPlaceholderImage:self.placeholder];
// 通过helper来处理逻辑,cell只需知道要设置本地图片或网络图片
if (helper.isLoaclImage) {
[cell setImage:helper.localImage];
}else{
[cell setImageWithURL:helper.downloadURL placeholderImage:helper.placeholderImage];
}
return cell;
}
#pragma mark - scrollView代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//获取当前偏移量
int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
self.pageControl.currentPage = page;
self.indicatorLabel.text = [NSString stringWithFormat:@"%d/%ld",page + 1,_imagesCount];
[self.indicatorLabel sizeToFit];
}
#pragma mark - YCPhotoBrowserCellDelegate
- (void)longPressPhotoBrowserCell:(YCPhotoBrowserCell *)cell{
if (self.longPressBlock) {
self.longPressBlock();
}
}
- (void)singleTapPhotoBrowserCell:(YCPhotoBrowserCell *)cell{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - AnimatorDismissedDelegate
- (NSIndexPath *)indexPathForDimissView{
YCPhotoBrowserCell *cell = self.collectionView.visibleCells.firstObject;
return [self.collectionView indexPathForCell:cell];
}
- (UIImageView *)imageViewForDimissView{
UIImageView *animationView = [[UIImageView alloc] init];
YCPhotoBrowserCell *cell = self.collectionView.visibleCells.firstObject;
animationView.image = cell.imageView.image;
animationView.frame = cell.imageView.frame;
animationView.contentMode = UIViewContentModeScaleAspectFill;
animationView.clipsToBounds = YES;
return animationView;
}
#pragma mark - 属性
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
layout.itemSize = self.view.bounds.size;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.pagingEnabled = YES;
_collectionView.bounces = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
}
return _collectionView;
}
- (UIPageControl *)pageControl{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] init];
_pageControl.pageIndicatorTintColor = [UIColor grayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
_pageControl.numberOfPages = _imagesCount;
_pageControl.center = _indicatorCenter;
_pageControl.hidden = YES;
}
return _pageControl;
}
- (UILabel *)indicatorLabel{
if (!_indicatorLabel) {
_indicatorLabel = [[UILabel alloc] init];
_indicatorLabel.font = [UIFont systemFontOfSize:20];
_indicatorLabel.textColor = [UIColor whiteColor];
_indicatorLabel.center = _indicatorCenter;
_indicatorLabel.hidden = YES;
}
return _indicatorLabel;
}
- (void)setBrowserAnimator:(YCPhotoBrowserAnimator *)browserAnimator{
NSAssert(browserAnimator.presentedDelegate, @"please set presentedDelegate!");
_browserAnimator = browserAnimator;
_browserAnimator.dismissedDelegate = self;
_browserAnimator.indexPath = self.indexPath;
self.transitioningDelegate = browserAnimator;
self.modalPresentationStyle = UIModalPresentationCustom;
}
@end