简体   繁体   English

Swift Cocoa:通过动画更改NSScrollView帧大小

[英]Swift Cocoa: Change NSScrollView frame size with animation

The result I want to achieve is this: Kext Wizard : 我要实现的结果是:Kext向导:

在此处输入图片说明

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. 谢谢。

Ok problem solved by adding NSView to be set as boundary. 好了,通过添加NSView设置为边界解决了问题。 Here is full code and Xcode picture 这是完整的代码和Xcode图片

@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. 因此,以上代码可确保所有元素以相同的速度移动,只是问题gif看起来像。 All hidden elements should be in a small NSView to show up properly. 所有隐藏的元素都应该在一个小的NSView中以正确显示。

Xcode中 So the element will be invisible if it is outside of the NSView. 因此,如果元素在NSView之外,则它将是不可见的。 When the element moves into NSView range, it will not block other elements in the ViewController. 当元素移入NSView范围时,它将不会阻塞ViewController中的其他元素。 Problem solved! 问题解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中运行动画时更改节点大小 - Change node size while running animation in Swift 使用Cocoa / osx / swift更改图像的分辨率和大小(无移动应用程序) - change resolution and size of image with cocoa/osx/swift (no mobile apps) 可可 NSScrollView 不滚动 - Cocoa NSScrollView not scrolling 自定义相机视图-无法更改cameraLayout的帧大小UIImagePicker,Swift - Custom Camera View - Cannot change the frame size of the cameraLayout UIImagePicker, Swift cell.imageView!.frame大小更改。 迅速 - cell.imageView!.frame size change. Swift 由于字体大小的变化,Swift 标签不更新框架 - Swift label not updating frame due change of font size 为什么我不能在 Swift 中更改视图的帧大小? - Why can't I change the view's frame size in Swift? 如何使用swift或objective-c以编程方式更改macosx cocoa应用程序中系统光标的大小? - How to change the size of the system cursor in a macosx cocoa app programmatically using swift or objective-c? 在 NSScrollView (Swift) 中显示文本 - Displaying text in a NSScrollView (Swift) 使用淡入淡出动画更改 NSTextField 文本颜色 - Cocoa - Change NSTextField text color with fade animation - Cocoa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM