CALayer with NSScrollView, pan and click scaling

I have a CALayer hosting view that I would like to zoom in, scroll and click. If I paste it into the scrollview the scrolling works fine, but the scaling makes the content fly away all over the place. If I don't embed it in the scrollview, I can get the scaling to work nicely using CATransform3DMakeScale, but then I have problems panning and selecting objects.

What would be the recommended way to approach this - before I try to figure out the wrong option.

(If you have a source that does something like this, I'd really appreciate watching it.)

+3


source to share


1 answer


I was inspired by Apple Samples

Also see Optimizing drawing and scrolling in OS X WWDC 2013 video

CALayer

embedded in ScrollView

because it was ScrollView

not a Core Animation ( ScrollView.wantsLayer = true

) layer . Interface builder



Here's an example in Swift:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var win: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        win = NSWindow(contentRect: NSRect(x: 200, y: 200, width: 500, height: 500),
            styleMask: NSTitledWindowMask,
            backing: NSBackingStoreType.Buffered,
            defer: false)

        win.makeKeyAndOrderFront(win)

        let scrollView = NSScrollView()
        scrollView.allowsMagnification = true
        scrollView.hasHorizontalScroller = true
        scrollView.hasVerticalScroller = true
        scrollView.wantsLayer = true

        scrollView.contentView = CenteredClipView()
        scrollView.documentView = MyView()
        scrollView.backgroundColor = NSColor.controlColor()

        win.contentView = scrollView
    }
}

class MyView: NSView {
    var drawn = false

    override func updateLayer() {
        super.updateLayer()

        if !drawn {
            drawn = true

            frame = (superview! as NSView).frame

            var shape = CAShapeLayer()
            let p = CGPathCreateMutable()
            CGPathAddEllipseInRect(p, nil, frame)
            CGPathMoveToPoint(p, nil, bounds.maxX, 0)
            CGPathAddLineToPoint(p, nil, 0, bounds.maxY)
            shape.path = p
            shape.lineWidth = 5
            shape.fillColor = NSColor.whiteColor().CGColor
            shape.strokeColor = NSColor.selectedControlColor().CGColor
            shape.lineDashPattern = [5, 5]

            let ant = CABasicAnimation(keyPath: "lineDashPhase")
            ant.fromValue = 0
            ant.toValue = 1000000
            ant.duration = 10000
            ant.repeatCount = 10000
            shape.addAnimation(ant, forKey: "lineDashPhase")

            layer?.addSublayer(shape)
        }
    }
}

class CenteredClipView: NSClipView {
    override func constrainBoundsRect(proposedBounds: NSRect) -> NSRect {
        var rect = super.constrainBoundsRect(proposedBounds)

        if let containerView = documentView? as? NSView {
            if rect.size.width > containerView.frame.size.width {
                rect.origin.x = (containerView.frame.width - rect.width ) / 2
            }

            if rect.size.height > containerView.frame.size.height {
                rect.origin.y = (containerView.frame.height - rect.height ) / 2
            }
        }

        return rect
    }
}

      

+8


source







All Articles