[英]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中以正确显示。
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.