Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created October 2, 2022 23:19
Show Gist options
  • Save davidbalbert/65461c83237939f80d257f2cf3a00050 to your computer and use it in GitHub Desktop.
Save davidbalbert/65461c83237939f80d257f2cf3a00050 to your computer and use it in GitHub Desktop.
Default CGAffineTransforms in macOS
# Edited to align columns
GraphicsContext CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
GraphicsContext.withCGContext CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0)
FlippedView CGAffineTransform(a: 2.0, b: 0.0, c: 0.0, d: -2.0, tx: 0.0, ty: 1280.0)
NormalView CGAffineTransform(a: 2.0, b: 0.0, c: 0.0, d: 2.0, tx: 0.0, ty: 0.0)
class NormalView: NSView {
override func draw(_ dirtyRect: NSRect) {
print("NormalView", NSGraphicsContext.current!.cgContext.ctm)
}
}
struct NormalViewRepresentable: NSViewRepresentable {
func makeNSView(context: Context) -> NormalView {
NormalView()
}
func updateNSView(_ nsView: NormalView, context: Context) {
}
}
class FlippedView: NSView {
override var isFlipped: Bool {
true
}
override func draw(_ dirtyRect: NSRect) {
print("FlippedView", NSGraphicsContext.current!.cgContext.ctm)
}
}
struct FlippedViewRepresentable: NSViewRepresentable {
func makeNSView(context: Context) -> FlippedView {
FlippedView()
}
func updateNSView(_ nsView: FlippedView, context: Context) {
}
}
struct TransformTest: View {
var body: some View {
ZStack {
Canvas { context, size in
print("GraphicsContext", context.transform)
context.withCGContext { cgContext in
print("GraphicsContext.withCGContext", cgContext.ctm)
}
}
NormalViewRepresentable()
FlippedViewRepresentable()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment