Repository navigation
Expand file tree
/
Copy pathiPopUp.m
More file actions
200 lines (137 loc) · 6.59 KB
/
Copy pathiPopUp.m
File metadata and controls
200 lines (137 loc) · 6.59 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
//
// iPopUp.m
// iPopUp
//
// Created by Gabriel Gino Vincent on 23/01/13.
// Copyright (c) 2013 Sync. All rights reserved.
//
#define PopUpViewWidth popUpView.frame.size.width
#define PopUpViewHeight popUpView.frame.size.height
#define PopUpViewOriginX popUpView.frame.origin.x
#define PopUpViewOriginY popUpView.frame.origin.y
#define ContainerViewWidth self.frame.size.width
#define ContainerViewHeight self.frame.size.height
#import "iPopUp.h"
@implementation iPopUp
#pragma mark Initialization
- (id) init {
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
[self configureContainerView];
}
return self;
}
- (void) configureContainerView {
defaults = [[NSUserDefaults standardUserDefaults] init];
CGRect deviceScreenSize;
if ([UIApplication sharedApplication].statusBarHidden)
deviceScreenSize = [[UIScreen mainScreen] bounds];
else
deviceScreenSize = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-20);
self.frame = deviceScreenSize;
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
}
#pragma mark Implementation
- (void) drawBackgroundGradient {
CGFloat colors [] = {
0.5, 0.5, 0.5, 0.7,
0.0, 0.0, 0.0, 0.7
};
CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
CGColorSpaceRelease(baseSpace), baseSpace = NULL;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextDrawRadialGradient(context, gradient, self.center, 0, self.center, self.frame.size.width, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient), gradient = NULL;
CGContextRestoreGState(context);
}
- (void) drawRect:(CGRect)rect {
[self drawBackgroundGradient];
}
- (void) savePopUpIdentifier:(NSString *)identifier {
[defaults setObject:identifier forKey:@"IPOPUP_PopUpIdentifier"];
[defaults synchronize];
}
- (void) verifyIfShouldShow {
NSOperationQueue *queue = [NSOperationQueue new];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.checkURL cachePolicy:NSURLCacheStorageAllowed timeoutInterval:8.0];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data) {
NSString *identifier = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil] objectForKey:@"identifier"];
NSString *lastSavedIdentifier = [defaults stringForKey:@"IPOPUP_PopUpIdentifier"];
if ([identifier isEqualToString:lastSavedIdentifier])
return;
NSString *imageURLString = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil] objectForKey:@"url"];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
backgroundImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
[self show];
});
[self savePopUpIdentifier:identifier];
}
}];
}
- (void) startShowingAnimation {
float finalOriginX = (ContainerViewWidth - PopUpViewWidth) / 2.0;
float finalOriginY = ((ContainerViewHeight - PopUpViewHeight) / 6.0);
float spaceBetweenImageAndCloseButton = finalOriginY/2.0;
popUpViewFinalFrame = CGRectMake(finalOriginX, finalOriginY, PopUpViewWidth, PopUpViewHeight);
[UIView animateWithDuration:0.3 animations:^{
popUpView.frame = CGRectMake(finalOriginX, finalOriginY-20, PopUpViewWidth, PopUpViewHeight);;
popUpView.alpha = 1.0;
closeButton.frame = CGRectMake(PopUpViewOriginX, finalOriginY-30+PopUpViewHeight, PopUpViewWidth, 40);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
popUpView.frame = popUpViewFinalFrame;
closeButton.frame = CGRectMake(PopUpViewOriginX, finalOriginY+PopUpViewHeight + spaceBetweenImageAndCloseButton, PopUpViewWidth, 40);
} completion:^(BOOL finished) {
}];
}];
}
- (void) close {
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.2 animations:^{
popUpView.alpha = 0.0;
closeButton.alpha = 0.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}];
});
}
- (void) show {
NSLog(@"Showing");
float reducedWidth = ContainerViewWidth-(ContainerViewWidth*0.2);
float reducedHeight = ContainerViewHeight-(ContainerViewHeight*0.2);
float initialOriginX = (ContainerViewWidth-reducedWidth)/2.0;
float inititalOriginY = ContainerViewHeight;
popUpView = [[UIView alloc] initWithFrame:CGRectMake(initialOriginX, inititalOriginY, reducedWidth, reducedHeight)];
[self addSubview:popUpView];
[self.delegate.view addSubview:self];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, reducedWidth, reducedHeight)];
backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
backgroundImageView.image = backgroundImage;
[popUpView addSubview:backgroundImageView];
float closeButtonOriginY = PopUpViewOriginY+PopUpViewHeight;
closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.titleLabel.text = @"Close";
closeButton.frame = CGRectMake(PopUpViewOriginX, closeButtonOriginY, PopUpViewWidth, 40);
[closeButton setBackgroundImage:self.closeButtonImage forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
self.alpha = 0.0;
popUpView.alpha = 0.0;
[self addSubview:closeButton];
[UIView animateWithDuration:0.2 animations:^{
self.alpha = 1.0;
} completion:^(BOOL finished) {
[self startShowingAnimation];
}];
}
@end