如何从NSGraphicsContext获取NSAffineTransformation

时间:2011-11-22 20:43:17

标签: objective-c cocoa

我获得了[NSGraphicsContext currentContext]的上下文,如何获取当前应用的NSAffineTransform

我需要这个来实现我自己的转换堆栈,我可以在OpenGl中执行pushpop操作,就像glPushMatrix()glPopMatrix()一样。

[NSGraphicsContext saveGraphicsState]不会为我做,因为它不可堆叠,或者是它?

1 个答案:

答案 0 :(得分:3)

是的。在将更改应用于上下文之前保存图形状态,然后恢复状态。每个saveGraphicsState必须与相应的restoreGraphicsState电话配对。

// Initial state

[NSGraphicsContext saveGraphicsState];

[transform1 concat];
// State 1.
//Draw with transform1 concatenated with the current transform of the context

[NSGraphicsContext saveGraphicsState];

[transform2 set];
// State 2
// draw with transform2 that replaces the transform of the context

[NSGraphicsContext restoreGraphicsState];

// now we have State 1 again

[NSGraphicsContext restoreGraphicsState];

// We came back to the initial state