Skip to content

Instantly share code, notes, and snippets.

@zenangst
Created February 26, 2018 20:06
Show Gist options
  • Save zenangst/a91002d5300f068f3d97164d721bf41b to your computer and use it in GitHub Desktop.
Save zenangst/a91002d5300f068f3d97164d721bf41b to your computer and use it in GitHub Desktop.
How scroll views work on macOS - Example 2
import Cocoa
import PlaygroundSupport
class View: NSView {
override var isFlipped: Bool { return true }
}
let subviewFrame = CGRect(origin: .zero,
size: CGSize(width: 320, height: 640 * 4))
let gradient = CAGradientLayer()
gradient.colors = [
NSColor.blue.withAlphaComponent(0.2).cgColor,
NSColor.blue.withAlphaComponent(0.4).cgColor
]
gradient.frame = subviewFrame
let documentView = View(frame: subviewFrame)
documentView.wantsLayer = true
documentView.layer?.addSublayer(gradient)
let scrollViewFrame = CGRect(origin: .zero,
size: CGSize(width: 320, height: 640))
let scrollView = NSScrollView(frame: scrollViewFrame)
scrollView.backgroundColor = NSColor.green.withAlphaComponent(0.2)
scrollView.documentView = documentView
scrollView.contentView.scroll(to: .zero)
PlaygroundPage.current.liveView = scrollView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment