-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathImageView.swift
134 lines (110 loc) · 4.24 KB
/
ImageView.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// ImageView.swift
// TGUIKit
//
// Created by keepcoder on 22/09/16.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
public enum ImageViewTransition {
case `default`
case modern
}
open class ImageView: NSView {
public var isEventLess: Bool = false
public var animationTransition: ImageViewTransition = .default
open var animates:Bool = false
open var image:CGImage? {
didSet {
let wasImage = self.layer?.contents != nil
self.layer?.contents = image
if animates {
if !wasImage {
self.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
} else {
animate()
}
}
}
}
open var nsImage:NSImage? {
didSet {
let wasImage = self.layer?.contents != nil
self.layer?.contents = nsImage
if animates {
if !wasImage {
self.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
} else {
animate()
}
}
}
}
open var contentGravity: CALayerContentsGravity = .center {
didSet {
layer?.contentsGravity = contentGravity
}
}
open func sizeToFit() {
if let image = self.image {
setFrameSize(image.backingSize)
}
}
open override func hitTest(_ point: NSPoint) -> NSView? {
if isEventLess {
let view = super.hitTest(point)
if let view = view as? View {
if view.isEventLess || view === self {
return nil
}
}
if let view = view as? ImageView {
if view.isEventLess || view === self {
return nil
}
}
return view
} else {
return super.hitTest(point)
}
}
open override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
open override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
}
override public init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
layerContentsRedrawPolicy = .never
layer?.masksToBounds = true
}
init() {
super.init(frame: .zero)
self.wantsLayer = true
layerContentsRedrawPolicy = .never
layer?.masksToBounds = true
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func animate() -> Void {
let animation = CABasicAnimation(keyPath: "contents")
animation.duration = 0.2
self.layer?.add(animation, forKey: "contents")
}
override open func viewDidChangeBackingProperties() {
if let window = self.window {
}
}
open func change(pos position: NSPoint, animated: Bool, _ save:Bool = true, removeOnCompletion: Bool = true, duration:Double = 0.2, timingFunction: CAMediaTimingFunctionName = CAMediaTimingFunctionName.easeOut, completion:((Bool)->Void)? = nil) -> Void {
super._change(pos: position, animated: animated, save, removeOnCompletion: removeOnCompletion, duration: duration, timingFunction: timingFunction, completion: completion)
}
open func change(size: NSSize, animated: Bool, _ save:Bool = true, removeOnCompletion: Bool = true, duration:Double = 0.2, timingFunction: CAMediaTimingFunctionName = CAMediaTimingFunctionName.easeOut, completion:((Bool)->Void)? = nil) {
super._change(size: size, animated: animated, save, removeOnCompletion: removeOnCompletion, duration: duration, timingFunction: timingFunction, completion: completion)
}
open func change(opacity to: CGFloat, animated: Bool = true, _ save:Bool = true, removeOnCompletion: Bool = true, duration:Double = 0.2, timingFunction: CAMediaTimingFunctionName = CAMediaTimingFunctionName.easeOut, completion:((Bool)->Void)? = nil) {
super._change(opacity: to, animated: animated, save, removeOnCompletion: removeOnCompletion, duration: duration, timingFunction: timingFunction, completion: completion)
}
}