-
Notifications
You must be signed in to change notification settings - Fork 325
/
StatusTextField.swift
116 lines (86 loc) · 3.03 KB
/
StatusTextField.swift
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
//
// StatusTextField.swift
// AndroidTool
//
// Created by Morten Just Petersen on 11/24/15.
// Copyright © 2015 Morten Just Petersen. All rights reserved.
//
import Cocoa
class StatusTextField: NSTextField {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
func setup(){
wantsLayer = true
}
func animateIn(_ completion:@escaping ()->Void?){
let moveTo = (layer?.frame.origin.y)! - 5
let move = CABasicAnimation(keyPath: "position.y")
move.toValue = moveTo
move.duration = 0.2
move.isRemovedOnCompletion = true
move.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
let fade = CABasicAnimation(keyPath: "opacity")
fade.fromValue = 0
fade.toValue = 1
fade.duration = 0.2
fade.isRemovedOnCompletion = true
fade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
CATransaction.begin()
CATransaction.setCompletionBlock { () -> Void in
completion()
}
layer?.add(fade, forKey: "basicOpacity")
layer?.opacity = 1
layer?.add(move, forKey: "basicMove")
layer?.frame.origin.y = moveTo
CATransaction.commit()
}
func animateOut(_ completion:@escaping ()->Void){
let moveTo = (layer?.frame.origin.y)! + 5
let move = CABasicAnimation(keyPath: "position.y")
move.toValue = moveTo
move.duration = 0.2
move.isRemovedOnCompletion = true
move.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
let fade = CABasicAnimation(keyPath: "opacity")
fade.fromValue = 1
fade.toValue = 0
fade.isRemovedOnCompletion = true
fade.duration = 0.2
fade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
CATransaction.begin()
CATransaction.setCompletionBlock { () -> Void in
completion()
}
layer?.add(fade, forKey: "basicOpacity")
layer?.opacity = 0
layer?.add(move, forKey: "basicMove")
layer?.frame.origin.y = moveTo
CATransaction.commit()
}
func setText(_ text:String, shouldFadeOut:Bool = true){
animateOut { () -> Void in
self.stringValue = text
if shouldFadeOut {
self.animateIn { () -> Void? in
self.slowlyDecay()
}
}
}
}
func slowlyDecay(){
wantsLayer = true
let anim = CABasicAnimation(keyPath: "opacity")
anim.fromValue = 1
anim.toValue = 0
anim.duration = 60 //* 5 // 5 mins
self.layer?.add(anim, forKey: "opacity")
alphaValue = 0
}
}