-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathXHImageViewer.m
More file actions
402 lines (321 loc) · 15.5 KB
/
Copy pathXHImageViewer.m
File metadata and controls
402 lines (321 loc) · 15.5 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// XHImageViewer.m
// XHImageViewer
//
// Created by 曾 宪华 on 14-2-17.
// Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507
// 本人QQ群(142557668). All rights reserved.
//
#import "XHImageViewer.h"
#import "XHViewState.h"
#import "XHZoomingImageView.h"
#define kXHImageViewerBaseTopToolBarTag 100
#define kXHImageViewerBaseBottomToolBarTag 200
@interface XHImageViewer () <UIScrollViewDelegate>
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) NSArray *imgViews;
@property(nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
@property(nonatomic, copy) WillDismissWithSelectedViewBlock willDismissWithSelectedViewBlock;
@property(nonatomic, copy) DidDismissWithSelectedViewBlock didDismissWithSelectedViewBlock;
@property(nonatomic, copy) DidChangeToImageViewBlock didChangeToImageViewBlock;
@end
@implementation XHImageViewer
- (void)setImageViewsFromArray:(NSArray *)views {
NSMutableArray *imgViews = [NSMutableArray array];
for (id obj in views) {
if ([obj isKindOfClass:[UIImageView class]]) {
[imgViews addObject:obj];
UIImageView *view = obj;
XHViewState *state = [XHViewState viewStateForView:view];
[state setStateWithView:view];
view.userInteractionEnabled = NO;
}
}
_imgViews = [imgViews copy];
}
- (void)showWithImageViews:(NSArray *)views
selectedView:(UIImageView *)selectedView {
[self setImageViewsFromArray:views];
if (_imgViews.count > 0) {
if (![selectedView isKindOfClass:[UIImageView class]] ||
![_imgViews containsObject:selectedView]) {
selectedView = _imgViews[0];
}
[self showWithSelectedView:selectedView];
}
}
#pragma mark - Life Cycle
- (void)_setup {
self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1];
self.backgroundScale = 0.95;
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestureRecognizer:)];
_panGestureRecognizer.maximumNumberOfTouches = 1;
[self addGestureRecognizer:_panGestureRecognizer];
}
- (id)init {
return [self initWithFrame:CGRectZero];
}
- (id)initWithImageViewerWillDismissWithSelectedViewBlock:(WillDismissWithSelectedViewBlock)willDismissWithSelectedViewBlock
didDismissWithSelectedViewBlock:(DidDismissWithSelectedViewBlock)didDismissWithSelectedViewBlock
didChangeToImageViewBlock:(DidChangeToImageViewBlock)didChangeToImageViewBlock {
if (self = [self initWithFrame:CGRectZero]) {
self.willDismissWithSelectedViewBlock = willDismissWithSelectedViewBlock;
self.didDismissWithSelectedViewBlock = didDismissWithSelectedViewBlock;
self.didChangeToImageViewBlock = didChangeToImageViewBlock;
[self _setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
if (self) {
[self _setup];
}
return self;
}
#pragma mark - Properties
- (void)setBackgroundColor:(UIColor *)backgroundColor {
[super setBackgroundColor:[backgroundColor colorWithAlphaComponent:0]];
}
- (NSInteger)pageIndex {
return (_scrollView.contentOffset.x / _scrollView.frame.size.width + 0.5);
}
#pragma mark - Getter Method
- (UIView *)topToolBar {
UIView *topToolBar = [self viewWithTag:kXHImageViewerBaseTopToolBarTag];
if (!topToolBar) {
if ([self.delegate respondsToSelector:@selector(customTopToolBarOfImageViewer:)]) {
topToolBar = [self.delegate customTopToolBarOfImageViewer:self];
topToolBar.frame = CGRectMake(0, -CGRectGetHeight(topToolBar.bounds), CGRectGetWidth(topToolBar.bounds), CGRectGetHeight(topToolBar.bounds));
}
}
return topToolBar;
}
- (UIView *)bottomToolBar {
UIView *bottomToolBar = [self viewWithTag:kXHImageViewerBaseBottomToolBarTag];
if (!bottomToolBar) {
if ([self.delegate respondsToSelector:@selector(customBottomToolBarOfImageViewer:)]) {
bottomToolBar = [self.delegate customBottomToolBarOfImageViewer:self];
bottomToolBar.tag = kXHImageViewerBaseBottomToolBarTag;
bottomToolBar.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(bottomToolBar.bounds), CGRectGetHeight(bottomToolBar.bounds));
}
}
return bottomToolBar;
}
#pragma mark - View management
- (UIImageView *)currentView {
return [_imgViews objectAtIndex:self.pageIndex];
}
- (void)showWithSelectedView:(UIImageView *)selectedView {
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
const NSInteger currentPage = [_imgViews indexOfObject:selectedView];
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIView *topToolBar = [self topToolBar];
if (topToolBar) {
if (![self.subviews containsObject:topToolBar]) {
topToolBar.alpha = 0.0;
[self addSubview:topToolBar];
}
}
UIView *bottomToolBar = [self bottomToolBar];
if (bottomToolBar) {
if (![self.subviews containsObject:bottomToolBar]) {
bottomToolBar.alpha = 0.0;
[self addSubview:bottomToolBar];
}
}
CGRect scrollViewFrame = self.bounds;
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.backgroundColor =
[self.backgroundColor colorWithAlphaComponent:1];
_scrollView.alpha = 0;
_scrollView.delegate = self;
}
[self insertSubview:_scrollView atIndex:0];
[window addSubview:self];
const CGFloat fullW = window.frame.size.width;
const CGFloat fullH = window.frame.size.height;
selectedView.frame =
[window convertRect:selectedView.frame fromView:selectedView.superview];
[window addSubview:selectedView];
_panGestureRecognizer.enabled = NO;
[UIView animateWithDuration:0.3
animations:^{
_scrollView.alpha = 1;
window.rootViewController.view.transform = CGAffineTransformMakeScale(
self.backgroundScale, self.backgroundScale);
selectedView.transform = CGAffineTransformIdentity;
CGSize size = (selectedView.image) ? selectedView.image.size
: selectedView.frame.size;
CGFloat ratio = MIN(fullW / size.width, fullH / size.height);
CGFloat W = ratio * size.width;
CGFloat H = ratio * size.height;
selectedView.frame =
CGRectMake((fullW - W) / 2, (fullH - H) / 2, W, H);
}
completion:^(BOOL finished) {
_panGestureRecognizer.enabled = YES;
_scrollView.contentSize = CGSizeMake(_imgViews.count * fullW, 0);
_scrollView.contentOffset = CGPointMake(currentPage * fullW, 0);
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tappedScrollView:)];
[_scrollView addGestureRecognizer:gesture];
for (UIImageView *view in _imgViews) {
view.transform = CGAffineTransformIdentity;
CGSize size = (view.image) ? view.image.size : view.frame.size;
CGFloat ratio = MIN(fullW / size.width, fullH / size.height);
CGFloat W = ratio * size.width;
CGFloat H = ratio * size.height;
view.frame = CGRectMake((fullW - W) / 2, (fullH - H) / 2, W, H);
XHZoomingImageView *tmp = [[XHZoomingImageView alloc]
initWithFrame:CGRectMake([_imgViews indexOfObject:view] * fullW,
0, fullW, fullH)];
tmp.imageView = view;
[_scrollView addSubview:tmp];
}
[self showToolBar];
}];
}
- (void)showToolBar {
[UIView animateWithDuration:0.3 animations:^{
UIView *topToolBar = [self topToolBar];
UIView *bottomToolBar = [self bottomToolBar];
topToolBar.frame = CGRectMake(0, 0, CGRectGetWidth(topToolBar.bounds), CGRectGetHeight(topToolBar.bounds));
topToolBar.alpha = 1.0;
bottomToolBar.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - CGRectGetHeight(bottomToolBar.bounds), CGRectGetWidth(bottomToolBar.bounds), CGRectGetHeight(bottomToolBar.bounds));
bottomToolBar.alpha = 1.0;
} completion:^(BOOL finished) {
}];
}
- (void)dismissToolBar {
[UIView animateWithDuration:0.3 animations:^{
UIView *topToolBar = [self topToolBar];
UIView *bottomToolBar = [self bottomToolBar];
topToolBar.frame = CGRectMake(0, -CGRectGetHeight(topToolBar.bounds), CGRectGetWidth(topToolBar.bounds), CGRectGetHeight(topToolBar.bounds));
topToolBar.alpha = 0.0;
bottomToolBar.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(bottomToolBar.bounds), CGRectGetHeight(bottomToolBar.bounds));
bottomToolBar.alpha = 0.0;
} completion:^(BOOL finished) {
}];
}
- (void)prepareToDismiss {
UIImageView *currentView = [self currentView];
if ([self.delegate respondsToSelector:@selector(imageViewer:willDismissWithSelectedView:)]) {
[self.delegate imageViewer:self willDismissWithSelectedView:currentView];
}
if (self.willDismissWithSelectedViewBlock) {
self.willDismissWithSelectedViewBlock(self, currentView);
}
[self dismissToolBar];
for (UIImageView *view in _imgViews) {
if (view != currentView) {
XHViewState *state = [XHViewState viewStateForView:view];
view.transform = CGAffineTransformIdentity;
view.frame = state.frame;
view.transform = state.transform;
[state.superview addSubview:view];
}
}
}
- (void)dismissWithAnimate {
UIImageView *currentView = [self currentView];
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
CGRect rct = currentView.frame;
currentView.transform = CGAffineTransformIdentity;
currentView.frame = [window convertRect:rct fromView:currentView.superview];
[window addSubview:currentView];
[UIView animateWithDuration:0.3
animations:^{
_scrollView.alpha = 0;
window.rootViewController.view.transform = CGAffineTransformIdentity;
XHViewState *state = [XHViewState viewStateForView:currentView];
currentView.frame =
[window convertRect:state.frame fromView:state.superview];
currentView.transform = state.transform;
}
completion:^(BOOL finished) {
if (finished) {
XHViewState *state = [XHViewState viewStateForView:currentView];
currentView.transform = CGAffineTransformIdentity;
currentView.frame = state.frame;
currentView.transform = state.transform;
[state.superview addSubview:currentView];
for (UIView *view in _imgViews) {
XHViewState *_state = [XHViewState viewStateForView:view];
view.userInteractionEnabled = _state.userInteratctionEnabled;
}
[self removeFromSuperview];
if ([self.delegate
respondsToSelector:@selector(imageViewer:didDismissWithSelectedView:)]) {
[self.delegate imageViewer:self
didDismissWithSelectedView:currentView];
}
if (self.didDismissWithSelectedViewBlock) {
self.didDismissWithSelectedViewBlock(self, currentView);
}
}
}];
}
#pragma mark - Gesture events
- (void)tappedScrollView:(UITapGestureRecognizer *)sender {
if (self.disableTouchDismiss) {
return;
}
[self prepareToDismiss];
[self dismissWithAnimate];
}
- (void)handlePanGestureRecognizer:(UIPanGestureRecognizer *)sender {
if (self.disableTouchDismiss) {
return;
}
static UIImageView *currentView = nil;
if (sender.state == UIGestureRecognizerStateBegan) {
currentView = [self currentView];
UIView *targetView = currentView.superview;
while (![targetView isKindOfClass:[XHZoomingImageView class]]) {
targetView = targetView.superview;
}
if (((XHZoomingImageView *)targetView).isViewing) {
currentView = nil;
} else {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
currentView.frame =
[window convertRect:currentView.frame fromView:currentView.superview];
[window addSubview:currentView];
[self prepareToDismiss];
}
}
if (currentView) {
if (sender.state == UIGestureRecognizerStateEnded) {
if (_scrollView.alpha > 0.5) {
[self showWithSelectedView:currentView];
} else {
[self dismissWithAnimate];
}
currentView = nil;
} else {
CGPoint p = [sender translationInView:self];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, p.y);
transform = CGAffineTransformScale(transform, 1 - fabs(p.y) / 1000,
1 - fabs(p.y) / 1000);
currentView.transform = transform;
CGFloat r = 1 - fabs(p.y) / 200;
_scrollView.alpha = MAX(0, MIN(1, r));
}
}
}
#pragma mark - UIScrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([self.delegate respondsToSelector:@selector(imageViewer:didChangeToImageView:)]) {
[self.delegate imageViewer:self didChangeToImageView:[self currentView]];
}
if (self.didChangeToImageViewBlock) {
self.didChangeToImageViewBlock(self, [self currentView]);
}
}
@end