forked from myell0w/MTZoomWindow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTZoomWindow.m
432 lines (344 loc) · 16.6 KB
/
MTZoomWindow.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// MTZoomWindow.m
//
// Created by Matthias Tretter on 8.3.2011.
// Copyright (c) 2009-2011 Matthias Tretter, @myell0w. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Rotation code based on Alan Quatermains AQSelfRotatingViewController
#import "MTZoomWindow.h"
#import <QuartzCore/QuartzCore.h>
@interface MTZoomWindow () {
NSMutableSet *_zoomGestureRecognizers;
}
@property (nonatomic, strong, readwrite) UIView *zoomedView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *zoomContentView;
@property (unsafe_unretained, nonatomic, readonly) UIView *zoomSuperview;
@end
@implementation MTZoomWindow
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// setup window
self.windowLevel = UIWindowLevelStatusBar + 2.f;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor clearColor];
// setup black backgroundView
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.backgroundColor = [UIColor blackColor];
_backgroundView.alpha = 0.f;
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_backgroundView];
// setup scrollview
_maximumZoomScale = 2.f;
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.maximumZoomScale = _maximumZoomScale;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.delegate = self;
_scrollView.hidden = YES;
_scrollView.contentSize = CGSizeMake(1.f,1.f);
_scrollView.scrollEnabled = NO;
[self addSubview:_scrollView];
_zoomContentView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
_zoomContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_zoomContentView.userInteractionEnabled = NO;
[_scrollView addSubview:_zoomContentView];
// setup animation properties
_animationOptions = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
_animationDuration = 0.4;
_animationDelay = 0.;
_zoomGestureRecognizers = [NSMutableSet set];
// using setter on purpose here
self.zoomGestures = MTZoomGestureTap | MTZoomGesturePinch;
// iOS 6 Hacks: willChange and didChange won't get called after launching the Application
UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(statusBarOrientation)) {
[self setupForOrientation:UIInterfaceOrientationPortraitUpsideDown forceLayout:YES];
} else if (statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self setupForOrientation:UIInterfaceOrientationPortrait forceLayout:YES];
}
self.frame = [MTZoomWindow windowBounds];
// register for orientation change notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationWillChange:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationDidChange:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
////////////////////////////////////////////////////////////////////////
#pragma mark - MTZoomWindow
////////////////////////////////////////////////////////////////////////
- (void)zoomView:(UIView *)view toSize:(CGSize)size {
[self zoomView:view toSize:size completion:nil];
}
- (void)zoomView:(UIView *)view toSize:(CGSize)size completion:(dispatch_block_t)completion {
self.zoomedView = view;
// save frames before zoom operation
CGRect originalFrameInWindow = [view convertRect:view.bounds toView:self];
// pre-setup
self.backgroundView.alpha = 0.f;
self.hidden = NO;
// the zoomedView now has another superview and therefore we must change it's frame
// to still visually appear on the same place like before to the user
[self.zoomSuperview addSubview:self.zoomedView];
self.zoomedView.frame = originalFrameInWindow;
self.zoomedView.autoresizingMask = self.zoomedView.zoomedAutoresizingMask;
[UIView animateWithDuration:self.animationDuration
delay:self.animationDelay
options:self.animationOptions
animations:^{
self.backgroundView.alpha = 1.f;
self.zoomedView.frame = CGRectMake((self.bounds.size.width-size.width)/2.f, (self.bounds.size.height-size.height)/2.f,
size.width, size.height);
} completion:^(BOOL finished) {
[self makeKeyWindow];
id<MTZoomWindowDelegate> delegate = view.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:didZoomInView:)]) {
[delegate zoomWindow:self didZoomInView:view];
}
if (completion != nil) {
completion();
}
[self.scrollView flashScrollIndicators];
}];
}
- (void)zoomOut {
[self zoomOut:nil];
}
- (void)zoomOut:(dispatch_block_t)completion {
if (self.zoomedIn) {
CGRect destinationFrameInWindow = [self.zoomedView.zoomPlaceholderView convertRect:self.zoomedView.zoomPlaceholderView.bounds toView:self];
// reset zoom-scale of scrollView
[self.scrollView setZoomScale:1.f animated:YES];
[UIView animateWithDuration:self.animationDuration
delay:self.animationDelay
options:self.animationOptions | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.backgroundView.alpha = 0.0f;
self.zoomedView.frame = destinationFrameInWindow;
} completion:^(BOOL finished) {
// reset zoomed view to original position
self.zoomedView.frame = self.zoomedView.zoomPlaceholderView.frame;
self.zoomedView.autoresizingMask = self.zoomedView.zoomPlaceholderView.autoresizingMask;
[self.zoomedView.zoomPlaceholderView.superview insertSubview:self.zoomedView aboveSubview:self.zoomedView.zoomPlaceholderView];
[self.zoomedView.zoomPlaceholderView removeFromSuperview];
self.zoomedView.zoomPlaceholderView = nil;
// hide window
self.hidden = YES;
[self.zoomedView.window makeKeyWindow];
id<MTZoomWindowDelegate> delegate = self.zoomedView.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:didZoomOutView:)]) {
[delegate zoomWindow:self didZoomOutView:self.zoomedView];
}
if (completion != nil) {
completion();
}
self.zoomedView = nil;
}];
}
}
- (UIView *)zoomSuperview {
if (self.zoomedView.wrapInScrollviewWhenZoomed) {
self.scrollView.hidden = NO;
return self.zoomContentView;
} else {
self.scrollView.hidden = YES;
return self;
}
}
- (BOOL)isZoomedIn {
return !self.hidden && self.zoomedView != nil;
}
- (void)setZoomGestures:(MTZoomGestureMask)zoomGestures {
if (zoomGestures != _zoomGestures) {
_zoomGestures = zoomGestures;
// remove old gesture recognizers
for (UIGestureRecognizer *gestureRecognizer in _zoomGestureRecognizers) {
[self removeGestureRecognizer:gestureRecognizer];
}
[_zoomGestureRecognizers removeAllObjects];
// create new gesture recognizers
if (zoomGestures & MTZoomGestureTap) {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleGesture:)];
[_zoomGestureRecognizers addObject:tapGestureRecognizer];
}
if (zoomGestures & MTZoomGestureDoubleTap) {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleGesture:)];
tapGestureRecognizer.numberOfTapsRequired = 2;
[_zoomGestureRecognizers addObject:tapGestureRecognizer];
}
// add new gesture recognizers to views
for (UIGestureRecognizer *gestureRecognizer in _zoomGestureRecognizers) {
[self addGestureRecognizer:gestureRecognizer];
}
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrollViewDelegate
////////////////////////////////////////////////////////////////////////
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.zoomContentView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// if we zoomed in we want to allow panning around
if (scrollView.zoomScale > 1.f) {
scrollView.scrollEnabled = YES;
} else {
scrollView.scrollEnabled = NO;
}
if (self.zoomGestures & MTZoomGesturePinch) {
if (!scrollView.zooming && scrollView.zoomBouncing && scrollView.zoomScale <= 1.f) {
[self.zoomedView zoomOut];
}
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
+ (CGRect)windowBounds {
if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]) {
CGRect bounds = [UIScreen mainScreen].nativeBounds;
return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width / [UIScreen mainScreen].scale, bounds.size.height / [UIScreen mainScreen].scale);
}
return [UIScreen mainScreen].bounds;
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
[self.zoomedView zoomOut];
}
}
- (void)setupForOrientation:(UIInterfaceOrientation)orientation forceLayout:(BOOL)forceLayout {
UIInterfaceOrientation current = [[UIApplication sharedApplication] statusBarOrientation];
if (!forceLayout) {
// if ( [self shouldAutorotateToInterfaceOrientation: orientation] == NO )
// return;
if (current == orientation) {
return;
}
}
// direction and angle
CGFloat angle = 0.0;
switch (current) {
case UIInterfaceOrientationPortrait: {
switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
angle = (CGFloat)M_PI; // 180.0*M_PI/180.0 == M_PI
break;
case UIInterfaceOrientationLandscapeLeft:
angle = (CGFloat)(M_PI*-90.0)/180.0;
break;
case UIInterfaceOrientationLandscapeRight:
angle = (CGFloat)(M_PI*90.0)/180.0;
break;
default:
return;
}
break;
}
case UIInterfaceOrientationPortraitUpsideDown: {
switch (orientation) {
case UIInterfaceOrientationPortrait:
angle = (CGFloat)M_PI; // 180.0*M_PI/180.0 == M_PI
break;
case UIInterfaceOrientationLandscapeLeft:
angle = (CGFloat)(M_PI*90.0)/180.0;
break;
case UIInterfaceOrientationLandscapeRight:
angle = (CGFloat)(M_PI*-90.0)/180.0;
break;
default:
return;
}
break;
}
case UIInterfaceOrientationLandscapeLeft: {
switch (orientation) {
case UIInterfaceOrientationLandscapeRight:
angle = (CGFloat)M_PI; // 180.0*M_PI/180.0 == M_PI
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = (CGFloat)(M_PI*-90.0)/180.0;
break;
case UIInterfaceOrientationPortrait:
angle = (CGFloat)(M_PI*90.0)/180.0;
break;
default:
return;
}
break;
}
case UIInterfaceOrientationLandscapeRight: {
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
angle = (CGFloat)M_PI; // 180.0*M_PI/180.0 == M_PI
break;
case UIInterfaceOrientationPortrait:
angle = (CGFloat)(M_PI*-90.0)/180.0;
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = (CGFloat)(M_PI*90.0)/180.0;
break;
default:
return;
}
break;
}
case UIInterfaceOrientationUnknown:
return;
}
CGAffineTransform rotation = CGAffineTransformMakeRotation(angle);
[UIView animateWithDuration:0.4 animations:^{
self.transform = CGAffineTransformConcat(rotation, self.transform);
}];
}
- (void)orientationWillChange:(NSNotification *)note {
UIInterfaceOrientation orientation = [[[note userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
[self.scrollView setZoomScale:1.f animated:YES];
[self setupForOrientation:orientation forceLayout:NO];
}
- (void)orientationDidChange:(NSNotification *)note {
// UIInterfaceOrientation orientation = [[[note userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
//if ([self shouldAutorotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]] == NO)
// return;
self.frame = [MTZoomWindow windowBounds];
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Singleton definitons
////////////////////////////////////////////////////////////////////////
static MTZoomWindow *sharedMTZoomWindow = nil;
+ (MTZoomWindow *)sharedWindow {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMTZoomWindow = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
});
return sharedMTZoomWindow;
}
@end