Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 对NSGraphicsContext.current所做的更改_Swift_Cocoa_Nsview_Drawrect - Fatal编程技术网

Swift 对NSGraphicsContext.current所做的更改

Swift 对NSGraphicsContext.current所做的更改,swift,cocoa,nsview,drawrect,Swift,Cocoa,Nsview,Drawrect,我正在从NSView的drawRect()方法内部对当前NSGraphicContext进行更改,一切正常。所做的任何更改都将通过后续调用drawRect()方法持久化 但是,我想做的是从另一个对象的方法中对当前上下文进行更改。这个方法实际上是从DrawRect()方法内部调用的。但这些变化并不是持久的 代码如下: class SomeView:NSView { override func drawRect(dirtyRect: NSRect) { // myAObject is

我正在从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.
  }
}
NSGraphicsContext.current!.compositeOperation = .copy
在这两个方法MyAObjectInstance.executeCode()和MyBObjectInstance.executeCode()中,我使用如下代码获取当前NSGraphicsContext:

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.
  }
}
NSGraphicsContext.current!.compositeOperation = .copy

我添加了一个改变前景色的小项目。在连续调用drawRect()之间,不会保留颜色更改。

问题是您希望在多次调用drawRect之间保留图形和更改;那是不会发生的。每次调用drawRect时,都会重置上下文的状态。那是故意的。因此,每次调用drawRect时,都需要将上下文的状态设置为所需的状态,并绘制以填充由dirtyRect参数指定的区域。

请发布一个。我(错误地)假设保留了任何更改。我正在尝试开发的程序,一直在使用鼠标输入绘制,我想多次设置上下文的状态可能会带来额外的延迟。但似乎没有别的办法。非常感谢你!!!!绘制faaaaaaaaaaar的成本超过了简单地设置属性的成本。(您还应将答案标记为正确。)