-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOvatarPreview.m
148 lines (107 loc) · 5.87 KB
/
OOvatarPreview.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
//
// OOvatarPreview.m
// Ovatar
//
// Created by Joe Barbour on 11/06/2018.
// Copyright © 2018 Ovatar. All rights reserved.
//
#import "OOvatarPreview.h"
@implementation OOvatarPreview
-(void)previewPresent:(UIImageView *)icon caption:(NSString *)caption {
UIApplication *application = [UIApplication sharedApplication];
oframe = [self.superview convertRect:icon.frame fromView:self];
if (self.rounded == 0) self.rounded = 2.0;
if (![container isDescendantOfView:application.delegate.window.superview]) {
container = [[UIView alloc] initWithFrame:application.delegate.window.bounds];
container.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.85];
profile = [[UIImageView alloc] initWithFrame:oframe];
profile.backgroundColor = [UIColor clearColor];
profile.clipsToBounds = true;
profile.image = icon.image;
profile.userInteractionEnabled = true;
profile.layer.cornerRadius = icon.layer.cornerRadius;
[application.delegate.window setWindowLevel:UIWindowLevelNormal];
[application.delegate.window addSubview:container];
[application.delegate.window addSubview:profile];
gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
gesture.enabled = true;
gesture.delegate = self;
[profile addGestureRecognizer:gesture];
logo = [[UIButton alloc] initWithFrame:CGRectMake(container.bounds.size.width - 48.0, container.bounds.size.height - 48.0, 20.0, 20.0)];
logo.backgroundColor = [UIColor clearColor];
logo.layer.cornerRadius = 4.0;
[logo.imageView setContentMode:UIViewContentModeScaleAspectFit];
[logo addTarget:self action:@selector(details) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:logo];
CGFloat aspectwidth = container.bounds.size.width / icon.image.size.width;
CGFloat aspectheight = container.bounds.size.height;
CGFloat aspectratio = MIN(aspectwidth, aspectheight);
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
if (container.bounds.size.width > 0 && container.bounds.size.height > 0) {
[profile.layer setCornerRadius:self.rounded];
[profile setFrame:CGRectMake
(5.0 + (container.bounds.size.width / 2) - ((icon.image.size.width * aspectratio) / 2),
(5.0 + container.bounds.size.height / 2) - ((icon.image.size.height * aspectratio) / 2),
((icon.image.size.width * aspectratio) - 10.0),
((icon.image.size.height * aspectratio) - 10.0))];
}
} completion:^(BOOL finished) {
[self icon];
}];
}
}
-(void)previewUpdate:(UIImage *)image {
[profile setImage:image];
}
-(void)gesture:(UIPanGestureRecognizer *)gesture {
oposition = [gesture translationInView:container.superview];
if (gesture.state == UIGestureRecognizerStateChanged) {
[profile setCenter:CGPointMake(container.bounds.size.width / 2, container.center.y + (oposition.y / 4))];
[container setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.85 - ((fabs(oposition.y) / 1000.0))]];
}
else if (gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateEnded) {
if (oposition.y < -240.0 || oposition.y > 240.0) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[profile setFrame:oframe];
} completion:^(BOOL finished) {
[container removeFromSuperview];
[profile removeFromSuperview];
[self removeFromSuperview];
}];
}
else {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[profile setFrame:CGRectMake(5.0, (container.bounds.size.height / 2) - (profile.bounds.size.height / 2), profile.bounds.size.width, profile.bounds.size.height)];
} completion:nil];
}
}
}
-(void)icon {
NSUserDefaults *cache = [NSUserDefaults standardUserDefaults];
if ([cache objectForKey:@"ovatar_logo_action"] != nil) {
[logo setImage:[UIImage imageWithData:[cache objectForKey:@"ovatar_logo_action"]] forState:UIControlStateNormal];
}
else {
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://ovatar.io/assets/LogoIcon.png"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if ([UIImage imageWithData:data] != nil) {
[cache setObject:data forKey:@"ovatar_logo_action"];
[logo setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
}
}];
}];
[task resume];
}
}
-(void)details {
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"http://ovatar.io/"]];
if (@available(iOS 11.0, *)) {
safari.dismissButtonStyle = SFSafariViewControllerDismissButtonStyleDone;
}
safari.delegate = self;
[(UINavigationController *)self.window.rootViewController presentViewController:safari animated:true completion:^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:false];
}];
}
@end