Created
February 26, 2018 20:06
-
-
Save zenangst/a91002d5300f068f3d97164d721bf41b to your computer and use it in GitHub Desktop.
How scroll views work on macOS - Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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