-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUIView+MTZoom.m
159 lines (117 loc) · 4.87 KB
/
UIView+MTZoom.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
//
// UIView+MTZoom.m
// MTZoomWindow
//
// Created by Tretter Matthias on 23.10.11.
// Copyright (c) 2011 NOUS Wissensmanagement GmbH. All rights reserved.
//
#import "UIView+MTZoom.h"
#import "MTZoomWindow.h"
#import <objc/runtime.h>
#define kMTDefaultZoomedAutoresizingMask UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
static char wrapKey;
static char sizeKey;
static char autoresizingKey;
static char placeholderKey;
static char delegateKey;
@implementation UIView (MTZoom)
////////////////////////////////////////////////////////////////////////
#pragma mark - Zooming
////////////////////////////////////////////////////////////////////////
- (void)zoomIn {
MTZoomWindow *zoomWindow = [MTZoomWindow sharedWindow];
if (!zoomWindow.zoomedIn) {
UIView *superview = self.superview;
UIView *placeholderView = [[UIView alloc] initWithFrame:self.frame];
// setup invisible copy of self
placeholderView.autoresizingMask = self.autoresizingMask;
[superview insertSubview:placeholderView belowSubview:self];
self.zoomPlaceholderView = placeholderView;
[self mt_callDelegateWillZoomIn];
// Zoom view into fullscreen-mode and call delegate
[zoomWindow zoomView:self toSize:self.zoomedSize];
}
}
- (void)zoomOut {
MTZoomWindow *zoomWindow = [MTZoomWindow sharedWindow];
[self mt_callDelegateWillZoomOut];
// zoom view back to original frame and call delegate
[zoomWindow zoomOut];
}
- (void)toggleZoomState {
if (self.zoomedIn) {
[self zoomOut];
} else {
[self zoomIn];
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Properties
////////////////////////////////////////////////////////////////////////
- (void)setWrapInScrollviewWhenZoomed:(BOOL)wrapInScrollviewWhenZoomed {
objc_setAssociatedObject(self, &wrapKey, @(wrapInScrollviewWhenZoomed), OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)isWrappedInScrollviewWhenZoomed {
BOOL wrapSetByUser = [objc_getAssociatedObject(self, &wrapKey) boolValue];
// scrollviews don't get wrapped in another scrollview
return wrapSetByUser && ![self isKindOfClass:[UIScrollView class]] && ![self isKindOfClass:NSClassFromString(@"MKMapView")];
}
- (void)setZoomedSize:(CGSize)zoomedSize {
objc_setAssociatedObject(self, &sizeKey, [NSValue valueWithCGSize:zoomedSize], OBJC_ASSOCIATION_RETAIN);
}
- (CGSize)zoomedSize {
return [objc_getAssociatedObject(self, &sizeKey) CGSizeValue];
}
- (void)setZoomedAutoresizingMask:(UIViewAutoresizing)zoomedAutoresizingMask {
objc_setAssociatedObject(self, &autoresizingKey, @(zoomedAutoresizingMask), OBJC_ASSOCIATION_RETAIN);
}
- (UIViewAutoresizing)zoomedAutoresizingMask {
id autoresizingNumber = objc_getAssociatedObject(self, &autoresizingKey);
if (autoresizingNumber == nil) {
return kMTDefaultZoomedAutoresizingMask;
}
return (UIViewAutoresizing)[autoresizingNumber intValue];
}
- (void)setZoomDelegate:(id<MTZoomWindowDelegate>)zoomDelegate {
objc_setAssociatedObject(self, &delegateKey, zoomDelegate, OBJC_ASSOCIATION_ASSIGN);
}
- (id<MTZoomWindowDelegate>)zoomDelegate {
return objc_getAssociatedObject(self, &delegateKey);
}
- (BOOL)isZoomedIn {
return [MTZoomWindow sharedWindow].zoomedIn && [MTZoomWindow sharedWindow].zoomedView == self;
}
- (void)setZoomPlaceholderView:(UIView *)zoomPlaceholderView {
objc_setAssociatedObject(self, &placeholderKey, zoomPlaceholderView, OBJC_ASSOCIATION_RETAIN);
}
- (UIView *)zoomPlaceholderView {
return (UIView *)objc_getAssociatedObject(self, &placeholderKey);
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Private Delegate Calls
////////////////////////////////////////////////////////////////////////
- (void)mt_callDelegateWillZoomIn {
id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:willZoomInView:)]) {
[delegate zoomWindow:[MTZoomWindow sharedWindow] willZoomInView:self];
}
}
- (void)mt_callDelegateDidZoomIn {
id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:didZoomInView:)]) {
[delegate zoomWindow:[MTZoomWindow sharedWindow] didZoomInView:self];
}
}
- (void)mt_callDelegateWillZoomOut {
id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:willZoomOutView:)]) {
[delegate zoomWindow:[MTZoomWindow sharedWindow] willZoomOutView:self];
}
}
- (void)mt_callDelegateDidZoomOut {
id<MTZoomWindowDelegate> delegate = self.zoomDelegate;
if ([delegate respondsToSelector:@selector(zoomWindow:didZoomOutView:)]) {
[delegate zoomWindow:[MTZoomWindow sharedWindow] didZoomOutView:self];
}
}
@end