gpt4 book ai didi

swift cocoa : Change NSScrollView frame size with animation

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:20 24 4
gpt4 key购买 nike

我想要达到的结果是这样的:Kext 向导:

enter image description here

我可以更改帧大小但没有任何动画。到目前为止,这是我的简单代码(不太关心大小但专注于动画):

@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))
}

}

此外,如果有人可以提示如何在框架尺寸缩小时显示隐藏菜单,那就太好了。谢谢。

最佳答案

好的,通过添加 NSView 设置为边界解决了问题。这是完整的代码和 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()
}
}

所以上面的代码确保所有元素以相同的速度移动,就像问题 gif 看起来一样。所有隐藏的元素都应该在一个小的 NSView 中才能正确显示。

Xcode因此,如果该元素位于 NSView 之外,则该元素将不可见。当元素移动到 NSView 范围内时,它不会阻塞 ViewController 中的其他元素。问题解决了!

关于 swift cocoa : Change NSScrollView frame size with animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com