Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finished redesign

  • Loading branch information...
commit 1c6e3c5348732d04dda91a5b0cf4963f5708819d 1 parent d45d508
Matthias Tretter authored
10 MTZoomWindow.h
@@ -22,19 +22,21 @@ typedef enum {
22 22 MTZoomGesturePinch = 4
23 23 } MTZoomGesture;
24 24
  25 +typedef NSInteger MTZoomGestureMask;
  26 +
25 27 @interface MTZoomWindow : UIWindow <UIScrollViewDelegate>
26 28
27 29 @property (nonatomic, retain) UIView *backgroundView;
28   -@property (nonatomic, assign) NSInteger zoomGestures;
  30 +@property (nonatomic, assign) MTZoomGestureMask zoomGestures;
29 31 @property (nonatomic, assign) UIViewAnimationOptions animationOptions;
30 32 @property (nonatomic, assign) NSTimeInterval animationDuration;
31 33 @property (nonatomic, assign) NSTimeInterval animationDelay;
32   -
  34 +@property (nonatomic, assign) float maximumZoomScale;
33 35 @property (nonatomic, readonly, getter = isZoomedIn) BOOL zoomedIn;
34 36
35 37 + (MTZoomWindow *)sharedWindow;
36 38
37   -- (void)zoomView:(UIView *)view toSize:(CGSize)size completion:(mt_zoom_block)completionBlock;
38   -- (void)zoomOutWithCompletion:(mt_zoom_block)completionBlock;
  39 +- (void)zoomView:(UIView *)view toSize:(CGSize)size;
  40 +- (void)zoomOut;
39 41
40 42 @end
160 MTZoomWindow.m
@@ -11,7 +11,8 @@
11 11 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 12 // 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,
13 13 // 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.
14   -
  14 +//
  15 +// Rotation code based on Alan Quatermains AQSelfRotatingViewController
15 16
16 17 #import "MTZoomWindow.h"
17 18
@@ -26,6 +27,7 @@ - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
26 27
27 28 - (void)orientationWillChange:(NSNotification *)note;
28 29 - (void)orientationDidChange:(NSNotification *)note;
  30 +- (void)setupForOrientation:(UIInterfaceOrientation)orientation forceLayout:(BOOL)forceLayout;
29 31
30 32 @end
31 33
@@ -40,8 +42,7 @@ @implementation MTZoomWindow
40 42 @synthesize scrollView = scrollView_;
41 43 @synthesize zoomedView = zoomedView_;
42 44 @synthesize gestureRecognizers = gestureRecognizers_;
43   -
44   -#import <QuartzCore/QuartzCore.h>
  45 +@synthesize maximumZoomScale = maximumZoomScale_;
45 46
46 47 ////////////////////////////////////////////////////////////////////////
47 48 #pragma mark -
@@ -51,8 +52,8 @@ @implementation MTZoomWindow
51 52 - (id)initWithFrame:(CGRect)frame {
52 53 if ((self = [super initWithFrame:frame])) {
53 54 // setup window
54   - self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
55 55 self.windowLevel = UIWindowLevelStatusBar + 2.0f;
  56 + self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
56 57 self.backgroundColor = [UIColor clearColor];
57 58
58 59 // setup black backgroundView
@@ -63,12 +64,15 @@ - (id)initWithFrame:(CGRect)frame {
63 64 [self addSubview:backgroundView_];
64 65
65 66 // setup scrollview
  67 + maximumZoomScale_ = 2.f;
66 68 scrollView_ = [[UIScrollView alloc] initWithFrame:self.frame];
67 69 scrollView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
68   - scrollView_.maximumZoomScale = 2.0f;
  70 + scrollView_.maximumZoomScale = maximumZoomScale_;
69 71 scrollView_.showsVerticalScrollIndicator = NO;
70 72 scrollView_.showsHorizontalScrollIndicator = NO;
71 73 scrollView_.delegate = self;
  74 + scrollView_.hidden = YES;
  75 + [self addSubview:scrollView_];
72 76
73 77 // setup animation properties
74 78 animationOptions_ = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
@@ -87,9 +91,6 @@ - (id)initWithFrame:(CGRect)frame {
87 91 selector: @selector(orientationDidChange:)
88 92 name: UIApplicationDidChangeStatusBarOrientationNotification
89 93 object: nil];
90   -
91   - scrollView_.layer.borderWidth = 2.f;
92   - scrollView_.layer.borderColor = [UIColor blueColor].CGColor;
93 94 }
94 95
95 96 return self;
@@ -116,12 +117,11 @@ - (void)dealloc {
116 117 #pragma mark MTZoomWindow
117 118 ////////////////////////////////////////////////////////////////////////
118 119
119   -- (void)zoomView:(UIView *)view toSize:(CGSize)size completion:(mt_zoom_block)completionBlock {
  120 +- (void)zoomView:(UIView *)view toSize:(CGSize)size {
120 121 self.zoomedView = view;
121 122
122 123 // save frames before zoom operation
123   - CGRect originalFrameInWindow = [view convertRect:view.bounds toView:nil];
124   - CGSize zoomedSize = view.zoomedSize;
  124 + CGRect originalFrameInWindow = [view convertRect:view.bounds toView:self];
125 125
126 126 // pre-setup
127 127 self.backgroundView.alpha = 0.f;
@@ -131,69 +131,75 @@ - (void)zoomView:(UIView *)view toSize:(CGSize)size completion:(mt_zoom_block)co
131 131 // to still visually appear on the same place like before to the user
132 132 [self.zoomSuperview addSubview:self.zoomedView];
133 133 self.zoomedView.frame = originalFrameInWindow;
134   - self.zoomedView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  134 + self.zoomedView.autoresizingMask = self.zoomedView.zoomedAutoresizingMask;
135 135
136 136 [UIView animateWithDuration:self.animationDuration
137 137 delay:self.animationDelay
138 138 options:self.animationOptions
139 139 animations:^{
140 140 self.backgroundView.alpha = 1.f;
141   - self.zoomedView.frame = CGRectMake((self.frame.size.width-zoomedSize.width)/2.f, (self.frame.size.height-zoomedSize.height)/2.f,
142   - zoomedSize.width, zoomedSize.height);
  141 + self.zoomedView.frame = CGRectMake((self.bounds.size.width-size.width)/2.f, (self.bounds.size.height-size.height)/2.f,
  142 + size.width, size.height);
143 143 } completion:^(BOOL finished) {
144   - if (completionBlock != nil) {
145   - completionBlock();
  144 + id<MTZoomWindowDelegate> delegate = view.zoomDelegate;
  145 +
  146 + if ([delegate respondsToSelector:@selector(zoomWindow:didZoomInView:)]) {
  147 + [delegate zoomWindow:self didZoomInView:view];
146 148 }
147 149 }];
148 150 }
149 151
150   -- (void)zoomOutWithCompletion:(mt_zoom_block)completionBlock {
151   - CGRect destinationFrameInWindow = [self.zoomedView.zoomPlaceholderView convertRect:self.zoomedView.zoomPlaceholderView.bounds toView:nil];
152   - UIView *zoomSuperview = self.zoomSuperview;
153   -
154   - // if superview is a scrollView, reset zoom-scale
155   - if ([zoomSuperview respondsToSelector:@selector(setZoomScale:animated:)]) {
156   - [zoomSuperview performSelector:@selector(setZoomScale:animated:)
157   - withObject:[NSNumber numberWithFloat:1.f]
158   - withObject:[NSNumber numberWithBool:YES]];
  152 +- (void)zoomOut {
  153 + if (self.zoomedIn) {
  154 + CGRect destinationFrameInWindow = [self.zoomedView.zoomPlaceholderView convertRect:self.zoomedView.zoomPlaceholderView.bounds toView:self];
  155 + UIView *zoomSuperview = self.zoomSuperview;
  156 +
  157 + // if superview is a scrollView, reset zoom-scale
  158 + if ([zoomSuperview respondsToSelector:@selector(setZoomScale:animated:)]) {
  159 + [zoomSuperview performSelector:@selector(setZoomScale:animated:)
  160 + withObject:[NSNumber numberWithFloat:1.f]
  161 + withObject:[NSNumber numberWithBool:YES]];
  162 + }
  163 +
  164 + [UIView animateWithDuration:self.animationDuration
  165 + delay:self.animationDelay
  166 + options:self.animationOptions | UIViewAnimationOptionBeginFromCurrentState
  167 + animations:^{
  168 + self.backgroundView.alpha = 0.0f;
  169 + self.zoomedView.frame = destinationFrameInWindow;
  170 + } completion:^(BOOL finished) {
  171 + // reset zoomed view to original position
  172 + self.zoomedView.frame = self.zoomedView.zoomPlaceholderView.frame;
  173 + self.zoomedView.autoresizingMask = self.zoomedView.zoomPlaceholderView.autoresizingMask;
  174 + [self.zoomedView.zoomPlaceholderView.superview addSubview:self.zoomedView];
  175 + [self.zoomedView.zoomPlaceholderView removeFromSuperview];
  176 + self.zoomedView.zoomPlaceholderView = nil;
  177 + // hide window
  178 + self.hidden = YES;
  179 +
  180 + id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
  181 +
  182 + if ([delegate respondsToSelector:@selector(zoomWindow:didZoomOutView:)]) {
  183 + [delegate zoomWindow:self didZoomOutView:self.zoomedView];
  184 + }
  185 +
  186 + self.zoomedView = nil;
  187 + }];
159 188 }
160   -
161   - [UIView animateWithDuration:self.animationDuration
162   - delay:self.animationDelay
163   - options:self.animationOptions | UIViewAnimationOptionBeginFromCurrentState
164   - animations:^{
165   - self.backgroundView.alpha = 0.0f;
166   - self.zoomedView.frame = destinationFrameInWindow;
167   - } completion:^(BOOL finished) {
168   - // reset zoomed view to original position
169   - self.zoomedView.frame = self.zoomedView.zoomPlaceholderView.frame;
170   - self.zoomedView.autoresizingMask = self.zoomedView.zoomPlaceholderView.autoresizingMask;
171   - [self.zoomedView.zoomPlaceholderView.superview addSubview:self.zoomedView];
172   - [self.zoomedView.zoomPlaceholderView removeFromSuperview];
173   - self.zoomedView.zoomPlaceholderView = nil;
174   - // hide window
175   - self.hidden = YES;
176   -
177   - if (completionBlock != nil) {
178   - completionBlock();
179   - }
180   - }];
181 189 }
182 190
183 191 - (UIView *)zoomSuperview {
184 192 if (self.zoomedView.wrapInScrollviewWhenZoomed) {
185   - if (self.scrollView.superview == nil) {
186   - [self insertSubview:self.scrollView atIndex:1];
187   - }
  193 + self.scrollView.hidden = NO;
188 194 return self.scrollView;
189 195 } else {
190   - [self.scrollView removeFromSuperview];
  196 + self.scrollView.hidden = YES;
191 197 return self;
192 198 }
193 199 }
194 200
195 201 - (BOOL)isZoomedIn {
196   - return !self.hidden && self.backgroundView.alpha > 0.f;
  202 + return !self.hidden && self.zoomedView != nil;
197 203 }
198 204
199 205 - (void)setZoomGestures:(NSInteger)zoomGestures {
@@ -217,32 +223,17 @@ - (void)setZoomGestures:(NSInteger)zoomGestures {
217 223 tapGestureRecognizer.numberOfTapsRequired = 2;
218 224 [self.gestureRecognizers addObject:tapGestureRecognizer];
219 225 }
220   - if (zoomGestures & MTZoomGesturePinch) {
221   - UIPinchGestureRecognizer *pinchGestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self
222   - action:@selector(handleGesture:)] autorelease];
223   - [self.gestureRecognizers addObject:pinchGestureRecognizer];
224   - }
225 226
226 227 // add new gesture recognizers to views
227 228 for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers) {
228 229 [self.backgroundView addGestureRecognizer:gestureRecognizer];
229   - // TODO: add to zoomedView
230 230 }
231 231 }
232 232 }
233 233
234 234 - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
235 235 if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
236   - if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
237   - UIPinchGestureRecognizer *pinchGestureRecognizer = (UIPinchGestureRecognizer *)gestureRecognizer;
238   -
239   - if (pinchGestureRecognizer.scale < 1.0) {
240   - // TODO: how to set completion block?
241   - [self zoomOutWithCompletion:nil];
242   - }
243   - } else {
244   - [self zoomOutWithCompletion:nil];
245   - }
  236 + [self zoomOut];
246 237 }
247 238 }
248 239
@@ -255,20 +246,30 @@ - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
255 246 return self.zoomedView;
256 247 }
257 248
  249 +- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  250 + if (self.zoomGestures & MTZoomGesturePinch) {
  251 + if (!scrollView.zooming && scrollView.zoomBouncing && scrollView.zoomScale <= 1.f) {
  252 + [self zoomOut];
  253 + }
  254 + }
  255 +}
  256 +
258 257 ////////////////////////////////////////////////////////////////////////
259 258 #pragma mark -
260 259 #pragma mark Rotation
261 260 ////////////////////////////////////////////////////////////////////////
262 261
263   -- (void)orientationWillChange:(NSNotification *)note {
264   - UIInterfaceOrientation current = [[UIApplication sharedApplication] statusBarOrientation];
265   - UIInterfaceOrientation orientation = [[[note userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
266   -
267   - // if ( [self shouldAutorotateToInterfaceOrientation: orientation] == NO )
268   - // return;
  262 +- (void)setupForOrientation:(UIInterfaceOrientation)orientation forceLayout:(BOOL)forceLayout {
  263 + UIInterfaceOrientation current = [[UIApplication sharedApplication] statusBarOrientation];
269 264
270   - if ( current == orientation )
271   - return;
  265 + if (!forceLayout) {
  266 + // if ( [self shouldAutorotateToInterfaceOrientation: orientation] == NO )
  267 + // return;
  268 +
  269 + if (current == orientation) {
  270 + return;
  271 + }
  272 + }
272 273
273 274 // direction and angle
274 275 CGFloat angle = 0.0;
@@ -354,20 +355,25 @@ - (void)orientationWillChange:(NSNotification *)note {
354 355 }
355 356 }
356 357
357   - CGAffineTransform rotation = CGAffineTransformMakeRotation( angle );
  358 + CGAffineTransform rotation = CGAffineTransformMakeRotation(angle);
358 359
359 360 [UIView animateWithDuration:0.4 animations:^{
360 361 self.transform = CGAffineTransformConcat(rotation, self.transform);
361 362 }];
362 363 }
363 364
  365 +- (void)orientationWillChange:(NSNotification *)note {
  366 + UIInterfaceOrientation orientation = [[[note userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
  367 + [self setupForOrientation:orientation forceLayout:NO];
  368 +}
  369 +
364 370 - (void)orientationDidChange:(NSNotification *)note {
365 371 // UIInterfaceOrientation orientation = [[[note userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
366 372
367 373 //if ([self shouldAutorotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]] == NO)
368 374 // return;
369 375
370   - self.frame = [[UIScreen mainScreen] applicationFrame];
  376 + self.frame = [[UIScreen mainScreen] bounds];
371 377 }
372 378
373 379 ////////////////////////////////////////////////////////////////////////
14 MTZoomWindow.xcodeproj/project.pbxproj
@@ -9,6 +9,9 @@
9 9 /* Begin PBXBuildFile section */
10 10 392B15A81326592500BF254A /* MTZoomWindow.h in Headers */ = {isa = PBXBuildFile; fileRef = 392B15A61326592500BF254A /* MTZoomWindow.h */; };
11 11 392B15A91326592500BF254A /* MTZoomWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 392B15A71326592500BF254A /* MTZoomWindow.m */; };
  12 + 9B0D3741145F3B0E00DB01C5 /* UIView+MTZoom.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B0D373F145F3B0E00DB01C5 /* UIView+MTZoom.h */; };
  13 + 9B0D3742145F3B0E00DB01C5 /* UIView+MTZoom.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B0D3740145F3B0E00DB01C5 /* UIView+MTZoom.m */; };
  14 + 9B0D3746145F3BC400DB01C5 /* MTZoomWindowDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B0D3745145F3BC400DB01C5 /* MTZoomWindowDelegate.h */; };
12 15 AA747D9F0F9514B9006C5449 /* MTZoomWindow_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* MTZoomWindow_Prefix.pch */; };
13 16 AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
14 17 /* End PBXBuildFile section */
@@ -16,6 +19,10 @@
16 19 /* Begin PBXFileReference section */
17 20 392B15A61326592500BF254A /* MTZoomWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTZoomWindow.h; sourceTree = "<group>"; };
18 21 392B15A71326592500BF254A /* MTZoomWindow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTZoomWindow.m; sourceTree = "<group>"; };
  22 + 9B0D373F145F3B0E00DB01C5 /* UIView+MTZoom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+MTZoom.h"; sourceTree = "<group>"; };
  23 + 9B0D3740145F3B0E00DB01C5 /* UIView+MTZoom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+MTZoom.m"; sourceTree = "<group>"; };
  24 + 9B0D3744145F3B1400DB01C5 /* Readme.mdown */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Readme.mdown; sourceTree = "<group>"; };
  25 + 9B0D3745145F3BC400DB01C5 /* MTZoomWindowDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTZoomWindowDelegate.h; sourceTree = "<group>"; };
19 26 AA747D9E0F9514B9006C5449 /* MTZoomWindow_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTZoomWindow_Prefix.pch; sourceTree = SOURCE_ROOT; };
20 27 AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
21 28 D2AAC07E0554694100DB518D /* libMTZoomWindow.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMTZoomWindow.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -65,6 +72,9 @@
65 72 children = (
66 73 392B15A61326592500BF254A /* MTZoomWindow.h */,
67 74 392B15A71326592500BF254A /* MTZoomWindow.m */,
  75 + 9B0D3745145F3BC400DB01C5 /* MTZoomWindowDelegate.h */,
  76 + 9B0D373F145F3B0E00DB01C5 /* UIView+MTZoom.h */,
  77 + 9B0D3740145F3B0E00DB01C5 /* UIView+MTZoom.m */,
68 78 );
69 79 name = Classes;
70 80 sourceTree = "<group>";
@@ -72,6 +82,7 @@
72 82 32C88DFF0371C24200C91783 /* Other Sources */ = {
73 83 isa = PBXGroup;
74 84 children = (
  85 + 9B0D3744145F3B1400DB01C5 /* Readme.mdown */,
75 86 AA747D9E0F9514B9006C5449 /* MTZoomWindow_Prefix.pch */,
76 87 );
77 88 name = "Other Sources";
@@ -86,6 +97,8 @@
86 97 files = (
87 98 AA747D9F0F9514B9006C5449 /* MTZoomWindow_Prefix.pch in Headers */,
88 99 392B15A81326592500BF254A /* MTZoomWindow.h in Headers */,
  100 + 9B0D3741145F3B0E00DB01C5 /* UIView+MTZoom.h in Headers */,
  101 + 9B0D3746145F3BC400DB01C5 /* MTZoomWindowDelegate.h in Headers */,
89 102 );
90 103 runOnlyForDeploymentPostprocessing = 0;
91 104 };
@@ -143,6 +156,7 @@
143 156 buildActionMask = 2147483647;
144 157 files = (
145 158 392B15A91326592500BF254A /* MTZoomWindow.m in Sources */,
  159 + 9B0D3742145F3B0E00DB01C5 /* UIView+MTZoom.m in Sources */,
146 160 );
147 161 runOnlyForDeploymentPostprocessing = 0;
148 162 };
27 MTZoomWindowDelegate.h
... ... @@ -0,0 +1,27 @@
  1 +//
  2 +// MTZoomWindowDelegate.h
  3 +//
  4 +// Created by Matthias Tretter on 31.10.2011.
  5 +// Copyright (c) 2009-2011 Matthias Tretter, @myell0w. All rights reserved.
  6 +//
  7 +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  8 +// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9 +// 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:
  10 +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12 +// 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,
  13 +// 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.
  14 +
  15 +@class MTZoomWindow;
  16 +
  17 +@protocol MTZoomWindowDelegate <NSObject>
  18 +
  19 +@optional
  20 +
  21 +- (void)zoomWindow:(MTZoomWindow *)zoomWindow willZoomInView:(UIView *)view;
  22 +- (void)zoomWindow:(MTZoomWindow *)zoomWindow didZoomInView:(UIView *)view;
  23 +
  24 +- (void)zoomWindow:(MTZoomWindow *)zoomWindow willZoomOutView:(UIView *)view;
  25 +- (void)zoomWindow:(MTZoomWindow *)zoomWindow didZoomOutView:(UIView *)view;
  26 +
  27 +@end
18 Readme.mdown
Source Rendered
@@ -10,21 +10,21 @@ Usage
10 10
11 11 You can use MTZoomWindow like this:
12 12
13   - // configure zoom window
14   - MTZoomWindow *zoomWindow = [[[MTZoomWindow alloc] initWithTargetView:targetView
15   - gestureRecognizerClass:[UITapGestureRecognizer class]] autorelease];
16 13 // specify size of the UIView when it is zoomed in
17   - zoomWindow.overlaySize = CGSizeMake(300,460);
18   - // customize gesture recognizer
19   - zoomWindow.windowGestureRecognizer.numberOfTapsRequired = 2;
20   - zoomWindow.zoomedViewGestureRecognizer.numberOfTapsRequired = 2;
21   - // That's it, when the user performs a double-tap on the targetView it get's zoomed
  14 + viewToZoom.zoomedSize = CGSizeMake(300,460);
  15 + viewToZoom.zoomedAutoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  16 + viewToZoom.wrapInScrollViewWhenZoomed = YES;
  17 +
  18 + // find this awesome shortcut for UITapGestureRecognizer in @zwaldowski's BlocksKit
  19 + [viewToZoom whenTapped:^{
  20 + [viewToZoom zoomIn];
  21 + }];
22 22
23 23
24 24 Adding MTZoomWindow to your project
25 25 ------------------
26 26
27   -1. Just copy over the files MTZoomWindow.h and MTZoomWindow.m to your project.
  27 +1. Just copy over the files MTZoomWindow.h, MTZoomWindow.m, UIView+MTZoom.h, UIView+MTZoom.m and MTZoomWindowDelegate.h to your project.
28 28 2. import "MTZoomWindow.h" everywhere where you want to use it
29 29 3. There is no step 3 :)
30 30
10 UIView+MTZoom.h
@@ -14,8 +14,7 @@
14 14
15 15
16 16 #import <UIKit/UIKit.h>
17   -
18   -typedef void (^mt_zoom_block)();
  17 +#import "MTZoomWindowDelegate.h"
19 18
20 19 @interface UIView (MTZoom)
21 20
@@ -23,13 +22,14 @@ typedef void (^mt_zoom_block)();
23 22 @property (nonatomic, assign, getter = isWrappedInScrollviewWhenZoomed) BOOL wrapInScrollviewWhenZoomed;
24 23 /** The size of the view when zoomed in */
25 24 @property (nonatomic, assign) CGSize zoomedSize;
  25 +/** The autoresizing-maks of the view when zoomed in */
  26 +@property (nonatomic, assign) UIViewAutoresizing zoomedAutoresizingMask;
26 27 /** the view that acts as a placeholder while self is zoomedIn */
27 28 @property (nonatomic, retain) UIView *zoomPlaceholderView;
  29 +/** The delegate for zooming */
  30 +@property (nonatomic, assign) id<MTZoomWindowDelegate> zoomDelegate;
28 31
29 32 - (void)zoomIn;
30 33 - (void)zoomOut;
31 34
32   -- (void)zoomInWithPreparation:(mt_zoom_block)preparationBlock completion:(mt_zoom_block)completionBlock;
33   -- (void)zoomOutWithPreparation:(mt_zoom_block)preparationBlock completion:(mt_zoom_block)completionBlock;
34   -
35 35 @end
117 UIView+MTZoom.m
@@ -10,9 +10,22 @@
10 10 #import "MTZoomWindow.h"
11 11 #import <objc/runtime.h>
12 12
  13 +#define kMTDefaultZoomedAutoresizingMask UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
  14 +
13 15 static char wrapKey;
14 16 static char sizeKey;
  17 +static char autoresizingKey;
15 18 static char placeholderKey;
  19 +static char delegateKey;
  20 +
  21 +@interface UIView ()
  22 +
  23 +- (void)mt_callDelegateWillZoomIn;
  24 +- (void)mt_callDelegateDidZoomIn;
  25 +- (void)mt_callDelegateWillZoomOut;
  26 +- (void)mt_callDelegateDidZoomOut;
  27 +
  28 +@end
16 29
17 30 @implementation UIView (MTZoom)
18 31
@@ -22,42 +35,31 @@ @implementation UIView (MTZoom)
22 35 ////////////////////////////////////////////////////////////////////////
23 36
24 37 - (void)zoomIn {
25   - [self zoomInWithPreparation:nil completion:nil];
26   -}
27   -
28   -- (void)zoomOut {
29   - [self zoomOutWithPreparation:nil completion:nil];
30   -}
31   -
32   -- (void)zoomInWithPreparation:(mt_zoom_block)preparationBlock completion:(mt_zoom_block)completionBlock {
33   - UIView *superview = self.superview;
34 38 MTZoomWindow *zoomWindow = [MTZoomWindow sharedWindow];
35   - UIView *placeholderView = [[[UIView alloc] initWithFrame:self.frame] autorelease];
36   -
37   - // setup invisible copy of self
38   - placeholderView.autoresizingMask = self.autoresizingMask;
39   - [superview insertSubview:placeholderView belowSubview:self];
40   - self.zoomPlaceholderView = placeholderView;
41 39
42   - // call preparation-block before we are zooming in
43   - if (preparationBlock != nil) {
44   - preparationBlock();
  40 + if (!zoomWindow.zoomedIn) {
  41 + UIView *superview = self.superview;
  42 + UIView *placeholderView = [[[UIView alloc] initWithFrame:self.frame] autorelease];
  43 +
  44 + // setup invisible copy of self
  45 + placeholderView.autoresizingMask = self.autoresizingMask;
  46 + [superview insertSubview:placeholderView belowSubview:self];
  47 + self.zoomPlaceholderView = placeholderView;
  48 +
  49 + [self mt_callDelegateWillZoomIn];
  50 +
  51 + // Zoom view into fullscreen-mode and call delegate
  52 + [zoomWindow zoomView:self toSize:self.zoomedSize];
45 53 }
46   -
47   - // Zoom view into fullscreen-mode and call completion-block
48   - [zoomWindow zoomView:self toSize:self.zoomedSize completion:completionBlock];
49 54 }
50 55
51   -- (void)zoomOutWithPreparation:(mt_zoom_block)preparationBlock completion:(mt_zoom_block)completionBlock {
  56 +- (void)zoomOut {
52 57 MTZoomWindow *zoomWindow = [MTZoomWindow sharedWindow];
53 58
54   - // call preparation-block before we are zooming out
55   - if (preparationBlock != nil) {
56   - preparationBlock();
57   - }
  59 + [self mt_callDelegateWillZoomOut];
58 60
59   - // zoom view back to original frame and call completion-block
60   - [zoomWindow zoomOutWithCompletion:completionBlock];
  61 + // zoom view back to original frame and call delegate
  62 + [zoomWindow zoomOut];
61 63 }
62 64
63 65 ////////////////////////////////////////////////////////////////////////
@@ -84,6 +86,28 @@ - (CGSize)zoomedSize {
84 86 return [objc_getAssociatedObject(self, &sizeKey) CGSizeValue];
85 87 }
86 88
  89 +- (void)setZoomedAutoresizingMask:(UIViewAutoresizing)zoomedAutoresizingMask {
  90 + objc_setAssociatedObject(self, &autoresizingKey, [NSNumber numberWithInt:zoomedAutoresizingMask], OBJC_ASSOCIATION_RETAIN);
  91 +}
  92 +
  93 +- (UIViewAutoresizing)zoomedAutoresizingMask {
  94 + id autoresizingNumber = objc_getAssociatedObject(self, &autoresizingKey);
  95 +
  96 + if (autoresizingNumber == nil) {
  97 + return kMTDefaultZoomedAutoresizingMask;
  98 + }
  99 +
  100 + return (UIViewAutoresizing)[autoresizingNumber intValue];
  101 +}
  102 +
  103 +- (void)setZoomDelegate:(id<MTZoomWindowDelegate>)zoomDelegate {
  104 + objc_setAssociatedObject(self, &delegateKey, zoomDelegate, OBJC_ASSOCIATION_ASSIGN);
  105 +}
  106 +
  107 +- (id<MTZoomWindowDelegate>)zoomDelegate {
  108 + return objc_getAssociatedObject(self, &delegateKey);
  109 +}
  110 +
87 111 - (void)setZoomPlaceholderView:(UIView *)zoomPlaceholderView {
88 112 objc_setAssociatedObject(self, &placeholderKey, zoomPlaceholderView, OBJC_ASSOCIATION_RETAIN);
89 113 }
@@ -92,4 +116,41 @@ - (UIView *)zoomPlaceholderView {
92 116 return (UIView *)objc_getAssociatedObject(self, &placeholderKey);
93 117 }
94 118
  119 +////////////////////////////////////////////////////////////////////////
  120 +#pragma mark -
  121 +#pragma mark Delegate Calls
  122 +////////////////////////////////////////////////////////////////////////
  123 +
  124 +- (void)mt_callDelegateWillZoomIn {
  125 + id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
  126 +
  127 + if ([delegate respondsToSelector:@selector(zoomWindow:willZoomInView:)]) {
  128 + [delegate zoomWindow:[MTZoomWindow sharedWindow] willZoomInView:self];
  129 + }
  130 +}
  131 +
  132 +- (void)mt_callDelegateDidZoomIn {
  133 + id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
  134 +
  135 + if ([delegate respondsToSelector:@selector(zoomWindow:didZoomInView:)]) {
  136 + [delegate zoomWindow:[MTZoomWindow sharedWindow] didZoomInView:self];
  137 + }
  138 +}
  139 +
  140 +- (void)mt_callDelegateWillZoomOut {
  141 + id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
  142 +
  143 + if ([delegate respondsToSelector:@selector(zoomWindow:willZoomOutView:)]) {
  144 + [delegate zoomWindow:[MTZoomWindow sharedWindow] willZoomOutView:self];
  145 + }
  146 +}
  147 +
  148 +- (void)mt_callDelegateDidZoomOut {
  149 + id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
  150 +
  151 + if ([delegate respondsToSelector:@selector(zoomWindow:didZoomOutView:)]) {
  152 + [delegate zoomWindow:[MTZoomWindow sharedWindow] didZoomOutView:self];
  153 + }
  154 +}
  155 +
95 156 @end

0 comments on commit 1c6e3c5

Please sign in to comment.
Something went wrong with that request. Please try again.