Sai Zhou
Sai Zhou

Reputation: 71

Swift Cocoa: Change NSScrollView frame size with animation

The result I want to achieve is this: Kext Wizard :

enter image description here

I can change the frame size but without any animation. Here is my simple code so far (didn't care too much about size but focus on animation):

@IBAction func disclosureButton(_ sender: Any) {
    if disclosureButton.state == .on {
        Swift.print("On")
        kextScroll.setFrameOrigin(kextScroll.frame.origin)
        kextScroll.setFrameSize(NSSize(width: 417, height: 316))
        //let rectBig = NSRect(origin: kextScroll.frame.origin, size: NSSize(width: 417, height: 316))
        //kextScroll.window?.setFrame(rectBig, display: true, animate: true)
    } else if disclosureButton.state == .off {
        Swift.print("Off")
        kextScroll.setFrameOrigin(kextScroll.frame.origin)
        kextScroll.setFrameSize(NSSize(width: 417, height: 200))
    }

}

Moreover if someone can give hint on how to show hidden menu as the frame size shrinks would be great. Thanks.

Upvotes: 4

Views: 302

Answers (1)

Sai Zhou
Sai Zhou

Reputation: 71

Ok problem solved by adding NSView to be set as boundary. Here is full code and Xcode picture

@IBAction func disclosureButton(_ sender: Any) {
    let dHeight = 70
    let smallOrigin = NSPoint(x: 20, y: 116)
    let bigOrigin = NSPoint(x: 20, y: 116-dHeight)
    let hideOrigin = NSPoint(x:0, y:25-dHeight)
    let showOrigin = NSPoint(x:0, y:25)
    if disclosureButton.state == .on {
        //hide
        NSAnimationContext.beginGrouping()
        NSAnimationContext.current.duration = 0.2
        kextScroll.animator().setFrameOrigin(bigOrigin)
        kextScroll.animator().setFrameSize(NSSize(width: 417, height: 249+dHeight))
        pathText.animator().setFrameOrigin(hideOrigin)
        reloadButton.animator().setFrameOrigin(NSPoint(x: 269, y:51-dHeight))
        unloadButton.animator().setFrameOrigin(NSPoint(x: 347, y:51-dHeight))
        exportButton.animator().setFrameOrigin(NSPoint(x: 269, y:-1-dHeight))
        revealButton.animator().setFrameOrigin(NSPoint(x: 347, y:-1-dHeight))
        kextLabel.animator().setFrameOrigin(NSPoint(x: 0, y:52-dHeight))
        NSAnimationContext.endGrouping()
    } else if disclosureButton.state == .off {
        //show
        NSAnimationContext.beginGrouping()
        NSAnimationContext.current.duration = 0.2
        kextScroll.animator().setFrameOrigin(smallOrigin)
        kextScroll.animator().setFrameSize(NSSize(width: 417, height: 249))
        pathText.animator().setFrameOrigin(showOrigin)
        reloadButton.animator().setFrameOrigin(NSPoint(x: 269, y:51))
        unloadButton.animator().setFrameOrigin(NSPoint(x: 347, y:51))
        exportButton.animator().setFrameOrigin(NSPoint(x: 269, y:-1))
        revealButton.animator().setFrameOrigin(NSPoint(x: 347, y:-1))
        kextLabel.animator().setFrameOrigin(NSPoint(x: 0, y:52))
        NSAnimationContext.endGrouping()
    }
}

So above code makes sure that all elements moves at same pace, just the question gif looks like. All hidden elements should be in a small NSView to show up properly.

Xcode So the element will be invisible if it is outside of the NSView. When the element moves into NSView range, it will not block other elements in the ViewController. Problem solved!

Upvotes: 1

Related Questions