对 NSGraphicsContext.current 所做的更改
Changes made to NSGraphicsContext.current
我正在从 NSView 的 drawRect() 方法内部对当前 NSGraphicContext 进行更改,一切正常。所做的任何更改都将在随后调用 drawRect() 方法时保持不变。
但是,我想做的是从另一个对象的方法中更改当前上下文。确实从 DrawRect() 方法内部调用了此方法。但是这些变化,并不是持久的。
代码是这样的:
class SomeView:NSView {
override func drawRect(dirtyRect: NSRect) {
// myAObject is an instance of a Class i create, and stored inside SomeView
// myAObject's exexcuteCode() method, makes changes to the current NSGraphicsContext
myAObjectIntance.executeCode()
// In subsequent calls of drawRect, as the user keeps drawing using the
// mouse, another object, say myBObject's executeCode() method, draws,
// lets say a rectangle.
myBObjectIntance.executeCode()
// Problem is, that any changes done to the current context by myAObjectIntance.executeCode()
// seem to be lost.
// If i change the context and draw inside the same method call, everything is OK.
// If changes and drawing are done by different objects, changes are lost.
}
}
从两个方法 myAObjectIntance.executeCode() 和 myBObjectIntance.executeCode() 中,我使用如下代码获取当前 NSGraphicsContext:
NSGraphicsContext.current!.compositeOperation = .copy
或
NSGraphicsContext.current!.compositeOperation = .clear
我添加了一个改变前景色的小项目。在连续调用 drawRect() 之间,不会保留颜色的变化。
NSGraphicsContextTest.zip
问题是您希望在多次调用 drawRect 之间保留您的绘图和更改;那不会发生。每次调用 drawRect 时,都会重置上下文的状态。这是设计使然。所以每次调用 drawRect 时,你需要将上下文的状态设置为任何你需要的状态,并绘制以填充 dirtyRect 参数指定的区域。
我正在从 NSView 的 drawRect() 方法内部对当前 NSGraphicContext 进行更改,一切正常。所做的任何更改都将在随后调用 drawRect() 方法时保持不变。
但是,我想做的是从另一个对象的方法中更改当前上下文。确实从 DrawRect() 方法内部调用了此方法。但是这些变化,并不是持久的。
代码是这样的:
class SomeView:NSView {
override func drawRect(dirtyRect: NSRect) {
// myAObject is an instance of a Class i create, and stored inside SomeView
// myAObject's exexcuteCode() method, makes changes to the current NSGraphicsContext
myAObjectIntance.executeCode()
// In subsequent calls of drawRect, as the user keeps drawing using the
// mouse, another object, say myBObject's executeCode() method, draws,
// lets say a rectangle.
myBObjectIntance.executeCode()
// Problem is, that any changes done to the current context by myAObjectIntance.executeCode()
// seem to be lost.
// If i change the context and draw inside the same method call, everything is OK.
// If changes and drawing are done by different objects, changes are lost.
}
}
从两个方法 myAObjectIntance.executeCode() 和 myBObjectIntance.executeCode() 中,我使用如下代码获取当前 NSGraphicsContext:
NSGraphicsContext.current!.compositeOperation = .copy
或
NSGraphicsContext.current!.compositeOperation = .clear
我添加了一个改变前景色的小项目。在连续调用 drawRect() 之间,不会保留颜色的变化。 NSGraphicsContextTest.zip
问题是您希望在多次调用 drawRect 之间保留您的绘图和更改;那不会发生。每次调用 drawRect 时,都会重置上下文的状态。这是设计使然。所以每次调用 drawRect 时,你需要将上下文的状态设置为任何你需要的状态,并绘制以填充 dirtyRect 参数指定的区域。