This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
/
SJView.m
executable file
·134 lines (99 loc) · 3.86 KB
/
SJView.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
// SJView.m
// Seb Jachec
#import "SJView.h"
#import <QuartzCore/QuartzCore.h>
@implementation SJView
#pragma mark - Basics
- (instancetype)initWithFrame:(NSRect)frame Color:(NSColor*)theColor {
self = [super initWithFrame:frame];
if (self) {
if (theColor) {
__weak SJView *wSelf = self;
[self setDrawBlock:^(void){
[theColor set];
NSRectFill(wSelf.bounds);
}];
}
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect Gradient:(NSGradient*)theGradient RelativeCenterPosition:(NSPoint)theCenter {
self = [super initWithFrame:frameRect];
if (self) {
if (theGradient) {
__weak SJView *wSelf = self;
[self setDrawBlock:^(void){
[theGradient drawInRect:wSelf.bounds relativeCenterPosition:theCenter];
}];
}
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect Gradient:(NSGradient*)theGradient Angle:(int)theAngle {
self = [super initWithFrame:frameRect];
if (self) {
if (theGradient) {
__weak SJView *wSelf = self;
[self setDrawBlock:^(void){
if (theAngle) {
[theGradient drawInRect:wSelf.bounds angle:theAngle];
} else {
[theGradient drawInRect:wSelf.bounds angle:-90];
}
}];
}
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
if (self.drawBlock) {
self.drawBlock();
} else if (self.baseDrawBlock) {
self.baseDrawBlock();
}
}
#pragma mark - Fade to Gradient
- (void)fadeToDrawBlock:(void (^)(void))newDrawBlock withDuration:(CGFloat)animDuration {
//Make an NSImageView with the current image
NSImageView *currentImageView = [[NSImageView alloc] initWithFrame:self.frame];
currentImageView.image = self.image;
[self addSubview:currentImageView];
//Make a new view drawing the new draw block, then get an image from it
SJView *tempView = [[SJView alloc] initWithFrame:self.frame];
tempView.drawBlock = newDrawBlock;
//Make a new NSImageView, with the new image
NSImageView *newImageView = [[NSImageView alloc] initWithFrame:self.frame];
newImageView.image = tempView.image;
[self setWantsLayer:YES];
NSDictionary *backupAnimations = self.animations;
//Setup the transition
CATransition *fade = [CATransition animation];
fade.duration = animDuration;
fade.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
fade.type = kCATransitionFade;
self.animations = @{@"subviews": fade};
//Start the animation, fading from the NSImageView with the current view, to the one with the new one.
[self replaceSubview:currentImageView with:newImageView];
self.drawBlock = newDrawBlock;
//Remove the NSImageView, to draw as normal
[newImageView removeFromSuperview];
//Restore previous animations
self.animations = backupAnimations;
}
- (void)fadeToDrawBlock:(void (^)(void))newDrawBlock {
[self fadeToDrawBlock:newDrawBlock withDuration:0.25];
}
#pragma mark - Image from View
- (NSImage *)image {
//Get the size the image needs to be - this current view's size
NSSize imgSize = NSMakeSize(self.bounds.size.width, self.bounds.size.height);
//Get an NSBitmapImageRep for everything in this view's bounds
NSBitmapImageRep *bitmapRep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
bitmapRep.size = imgSize;
[self cacheDisplayInRect:self.bounds toBitmapImageRep:bitmapRep];
//Return an NSImage, using the bitmap image representation (^)
NSImage *image = [[NSImage alloc] initWithSize:imgSize];
[image addRepresentation:bitmapRep];
return image;
}
@end