设置 NSView 的比例
Set scale of NSView
我将 NSView 子类化以使其可扩展。按照 Apple 的“View Geometry”指南,边界应拉伸以适合框架。
设置框架大小时,没有任何变化。当我将帧大小打印到控制台时,我看到 setFrameSize 几乎什么也没做。
所以我的问题是:如何更改设置的帧大小(不设置设置的边界大小)? (禁用自动版式)
class ScaledView : NSView {
let scale: CGFloat
let element: Drawable
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") // not needed, adding it programmatically
}
init(frame frameRect: NSRect, scale: CGFloat, toDraw element: DoubleValue) {
self.scale = scale
self.element = element;
super.init(frame: frameRect)
self.setFrameSize(CGSize(width: self.bounds.size.width * scale, height: self.bounds.size.height * scale))
}
override func drawRect(dirtyRect: NSRect) {
element.draw(dirtyRect, parent: self)
}
}
你确定你不是要设置边界,而不是框架?更改框架将改变视图在其父级坐标系中的大小,因此不仅会改变其内容的比例,还会改变其在父视图中的外观。如果您只是想缩放视图的内容,视图几何指南说:
To translate or scale the coordinate system, you alter the view's bounds rectangle. Changing the bounds rectangle sets up the basic coordinate system with which all drawing performed by the view begins. Concrete subclasses of NSView typically alter the bounds rectangle immediately as needed in their initWithFrame: methods or upon loading a nib file that contains the view.
我将 NSView 子类化以使其可扩展。按照 Apple 的“View Geometry”指南,边界应拉伸以适合框架。 设置框架大小时,没有任何变化。当我将帧大小打印到控制台时,我看到 setFrameSize 几乎什么也没做。
所以我的问题是:如何更改设置的帧大小(不设置设置的边界大小)? (禁用自动版式)
class ScaledView : NSView {
let scale: CGFloat
let element: Drawable
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") // not needed, adding it programmatically
}
init(frame frameRect: NSRect, scale: CGFloat, toDraw element: DoubleValue) {
self.scale = scale
self.element = element;
super.init(frame: frameRect)
self.setFrameSize(CGSize(width: self.bounds.size.width * scale, height: self.bounds.size.height * scale))
}
override func drawRect(dirtyRect: NSRect) {
element.draw(dirtyRect, parent: self)
}
}
你确定你不是要设置边界,而不是框架?更改框架将改变视图在其父级坐标系中的大小,因此不仅会改变其内容的比例,还会改变其在父视图中的外观。如果您只是想缩放视图的内容,视图几何指南说:
To translate or scale the coordinate system, you alter the view's bounds rectangle. Changing the bounds rectangle sets up the basic coordinate system with which all drawing performed by the view begins. Concrete subclasses of NSView typically alter the bounds rectangle immediately as needed in their initWithFrame: methods or upon loading a nib file that contains the view.