UI/NSScrollView Tips & Tricks

codelynx
January 14, 2017

UI/NSScrollView Tips & Tricks

Tips and Tricks of common problems of using scroll views. 1. make your scroll view crisp sharp on high zoom scale without crashing. 2. use scroll view with OpenGL or Metal related view.

codelynx

January 14, 2017

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

32 slides
  1. contentScaleFactor • OK – Device size like small contents •

    NG – Magazine / Newspaper like large contents • eg. 2048x1536 12MB — (4x zoom) — 8192 x 6144 48MB ⚠
  2. Convert rect from 
 content dummy view override func draw(_

    layer: CALayer, in ctx: CGContext) { UIGraphicsPushContext(ctx) ctx.saveGState() let box = page.getBoxRect(.cropBox) let rect = self.contentView.convert(self.contentView.bounds, to: self) ctx.translateBy(x: rect.minX, y: rect.minY) ctx.translateBy(x: 0, y: rect.height) ctx.scaleBy(x: 1, y: -1) ctx.scaleBy(x: rect.width / box.width, y: rect.height / box.height) ctx.drawPDFPage(page) ctx.restoreGState() UIGraphicsPopContext() } →
  3. UIScrollView delegate func viewForZooming(in scrollView: UIScrollView) -> UIView? { return

    contentView } func scrollViewDidZoom(_ scrollView: UIScrollView) { self.pdfPageView.setNeedsDisplay() } func scrollViewDidScroll(_ scrollView: UIScrollView) { self.pdfPageView.setNeedsDisplay() }
  4. Draw it class PDFPageView: UIView { // …
 override func

    setNeedsDisplay() { super.setNeedsDisplay() self.layer.setNeedsDisplay() } 
 override func layoutSubviews() { super.layoutSubviews() self.layer.drawsAsynchronously = true } // …
 }
  5. Not suitable to be a 
 subview of UI/NSScrollView •

    GLKView / EAGLLayer backed UI/NSView • MTKView / CAMetalLayer backed UI/NSView
  6. Features • No limitation for writing shaders – unlike SKShaders

    • No complex storyboard configuration – Just place RenderView • Subclass Scene or Canvas for your own displayable contents • Write your own shaders • Subclass Renderer to use your shaders • Ability to render Bezier Path with shaders • Possible Hybrid Displaying with Core Graphics

More Decks by codelynx

Other Decks in Programming