Skip to content

Instantly share code, notes, and snippets.

@zenangst
Created February 26, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenangst/66a4cbe0463b57e0c434cbfa61df48c8 to your computer and use it in GitHub Desktop.
Save zenangst/66a4cbe0463b57e0c434cbfa61df48c8 to your computer and use it in GitHub Desktop.
How scroll views work on macOS - Example 1
import Cocoa
import PlaygroundSupport
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 = NSView(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: CGPoint(x: 0, y: subviewFrame.size.height))
PlaygroundPage.current.liveView = scrollView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment