CALayer with NSScrollView, zooming panning and clicking

james_alvarez

I have a CALayer hosting view, which I would like to be able to zoom, scroll around and click in. If I embed it in a scrollview, the scrolling works fine, but zooming causes the contents to fly away everywhere. If I don't embed it in a scrollview, I can get the zooming working nicely using CATransform3DMakeScale, but then I have problems with panning, and being able to select objects.

What would be the recommended way to approach this - before I try and work it out with the wrong option.

(If you have any source that does a similar thing, I would be very grateful to see it.)

pravdomil

I got inspired here at Apple sample codes

Also look at the Optimizing Drawing and Scrolling on OS X WWDC 2013 video

The CALayer embedded in ScrollView flown because the ScrollView wasn't the Core Animation layer (ScrollView.wantsLayer = true). Interface Builder

Here 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
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Zooming and panning an image in a QScrollArea

Zooming and panning TextureView

Flot zooming and panning with logarithmic axis

Apply panning and zooming on inline SVG

zooming and panning with buttons in android emulator

Background image not zooming and panning with SVG

Is there a Google Map event listener for panning or zooming map?

Saving edited image after zooming, rotating and panning

SciChart - zooming and panning from code in MVVM

Zooming with mouse wheel breaks panning (scrolling) of real time data in ZedGraph

Having trouble panning and zooming on my d3 map

Zooming and Panning in JS/JQuery like we can do it using SVG

d3js zooming in one direction, panning in both directions

D3.js zooming and panning - lock on y axis

How to Implement linked zooming, panning in bokeh plots when embedded as Components

Constraining zooming and panning in a D3 map viewport

D3: Zooming/Panning Line Graph in SVG is not working in Canvas

D3 Canvas graph zooming and panning slow redraw in Webview

How do I intercept mapview panning and zooming (Swift 4)

How to stop ScrollView from scrolling when panning/zooming on a MapBox view?

amCharts 4 - How to Disable Scrollbar to Prevent Zooming and Panning

How to do panning/zooming operation in directx/c++

Shared matplotlib axes for images with different sizes (Linked zooming and panning)

R Shiny: googleway package - turn off auto zooming and panning during update

d3 v2 Prevent Zooming and Panning Chart Outside Viewport

How can I detect panning and zooming by the user with d3 zoomBehavior?

Best practice for zooming and panning HTML5 canvas with > 10k objects

How to check if my Cytoscape.js graph is out of view (even partially) after panning and/or zooming?

d3 how to tie text to top right corner of view port while zooming and panning