你如何翻转NSView的坐标系?

时间:2021-05-30 17:10:07

I have created an NSScrollView in interface builder that has a variable number of semi-unique NSViews that can be programmatically added and removed from it. When I add subViews to the documentView, they appear in the lower-left hand corner instead of the upper-left hand corner. I see that you can check the isFlipped bool to figure out if the view's coordinate system is flipped, but I cannot find a way to set it as flipped.

我在界面构建器中创建了一个NSScrollView,它具有可变数量的半唯一NSView,可以通过编程方式添加和删除它。当我将subViews添加到documentView时,它们出现在左下角而不是左上角。我看到你可以检查isFlipped bool来判断是否翻转了视图的坐标系,但我找不到将其设置为翻转的方法。

Anyone know what I'm missing?

谁知道我错过了什么?

3 个解决方案

#1


42  

In your NSView subclass, override the isFlipped method:

在NSView子类中,重写isFlipped方法:

isFlipped Returns YES if the receiver uses flipped drawing coordinates or NO if it uses native coordinates.

isFlipped如果接收器使用翻转的绘图坐标,则返回YES;如果使用本机坐标,则返回NO。

- (BOOL)isFlipped

- (BOOL)isFlipped

Discussion The default implementation returns NO; subclasses that use flipped coordinates should override this method to return YES.

讨论默认实现返回NO;使用翻转坐标的子类应重写此方法以返回YES。

#2


23  

For anyone wishing to do this in Swift here's how you override in your custom class:

对于希望在Swift中执行此操作的任何人,您可以在自定义类中覆盖:

class FlippedView: NSView {
    override var flipped:Bool {
        get {
            return true
        }
    }
}

#3


3  

The idea is that each individual view has its own way of drawing itself (for example, using human-calculated paths) that might get really wonky if you suddenly flip its coordinate plane (rasters might be fine while paths might draw upside-down, calculations might place things off-screen, etc.). So it's not settable, but subclasses can specify isFlipped because they should definitely know how the view is drawn.

这个想法是每个单独的视图都有自己的绘制方式(例如,使用人工计算的路径)如果你突然翻转它的坐标平面可能会变得非常不稳定(光栅可能会很好,而路径可能会倒置,计算可能会把东西放在屏幕外等等。所以它不可设置,但子类可以指定isFlipped,因为它们应该知道如何绘制视图。

Subclasses can also make it settable, but then they must expect it to change at any time.

子类也可以使它可设置,但是它们必须随时改变它。

As for code, here's Jay's answer in Swift 3:

至于代码,这里是Jay在Swift 3中的答案:

class FlippedView: NSView {
    override var isFlipped: Bool { return true }
}

#1


42  

In your NSView subclass, override the isFlipped method:

在NSView子类中,重写isFlipped方法:

isFlipped Returns YES if the receiver uses flipped drawing coordinates or NO if it uses native coordinates.

isFlipped如果接收器使用翻转的绘图坐标,则返回YES;如果使用本机坐标,则返回NO。

- (BOOL)isFlipped

- (BOOL)isFlipped

Discussion The default implementation returns NO; subclasses that use flipped coordinates should override this method to return YES.

讨论默认实现返回NO;使用翻转坐标的子类应重写此方法以返回YES。

#2


23  

For anyone wishing to do this in Swift here's how you override in your custom class:

对于希望在Swift中执行此操作的任何人,您可以在自定义类中覆盖:

class FlippedView: NSView {
    override var flipped:Bool {
        get {
            return true
        }
    }
}

#3


3  

The idea is that each individual view has its own way of drawing itself (for example, using human-calculated paths) that might get really wonky if you suddenly flip its coordinate plane (rasters might be fine while paths might draw upside-down, calculations might place things off-screen, etc.). So it's not settable, but subclasses can specify isFlipped because they should definitely know how the view is drawn.

这个想法是每个单独的视图都有自己的绘制方式(例如,使用人工计算的路径)如果你突然翻转它的坐标平面可能会变得非常不稳定(光栅可能会很好,而路径可能会倒置,计算可能会把东西放在屏幕外等等。所以它不可设置,但子类可以指定isFlipped,因为它们应该知道如何绘制视图。

Subclasses can also make it settable, but then they must expect it to change at any time.

子类也可以使它可设置,但是它们必须随时改变它。

As for code, here's Jay's answer in Swift 3:

至于代码,这里是Jay在Swift 3中的答案:

class FlippedView: NSView {
    override var isFlipped: Bool { return true }
}