forked from twitter-archive/twui
/
TUINSView+Hyperfocus.m
91 lines (71 loc) · 2.64 KB
/
TUINSView+Hyperfocus.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
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "TUINSView.h"
#import "TUINSView+Hyperfocus.h"
@implementation TUINSView (Hyperfocus)
- (void)endHyperFocus:(BOOL)cancel
{
if(_hyperFocusView) {
_hyperCompletion(cancel);
_hyperCompletion = nil;
TUIView *remove = _hyperFadeView;
[TUIView animateWithDuration:0.3 animations:^{
remove.alpha = 0.0;
} completion:^(BOOL finished) {
[remove removeFromSuperview];
}];
_hyperFadeView = nil;
_hyperFocusView = nil;
}
}
- (void)hyperFocus:(TUIView *)focusView completion:(void(^)(BOOL))completion
{
[self endHyperFocus:YES];
CGRect focusRect = [focusView frameInNSView];
CGFloat startRadius = 1.0;
CGFloat endRadius = MAX(self.rootView.bounds.size.width, self.rootView.bounds.size.height);
CGPoint center = CGPointMake(focusRect.origin.x + focusRect.size.width * 0.5, focusRect.origin.y + focusRect.size.height * 0.5);
TUIView *fade = [[TUIView alloc] initWithFrame:self.rootView.bounds];
fade.userInteractionEnabled = NO;
fade.autoresizingMask = TUIViewAutoresizingFlexibleSize;
fade.opaque = NO;
fade.drawRect = ^(TUIView *v, CGRect r) {
CGContextRef ctx = TUIGraphicsGetCurrentContext();
CGFloat locations[] = {0.0, 0.25, 1.0};
CGFloat components[] = {
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.15,
0.0, 0.0, 0.0, 0.55,
};
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locations, 3);
// CGContextSaveGState(ctx);
// CGContextClipToRoundRect(ctx, self.rootView.bounds, 9);
CGContextDrawRadialGradient(ctx, gradient, center, startRadius, center, endRadius, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
// CGContextRestoreGState(ctx);
CGGradientRelease(gradient);
CGColorSpaceRelease(space);
};
[CATransaction begin];
fade.alpha = 0.0;
[self.rootView addSubview:fade];
[CATransaction flush];
[CATransaction commit];
[TUIView animateWithDuration:0.2 animations:^{
fade.alpha = 1.0;
}];
_hyperFocusView = focusView;
_hyperFadeView = fade;
_hyperCompletion = [completion copy];
}
@end