0
我想请解释为什么XCode的OpenGl ES样本能够正常工作。它执行以下操作来启动并条机方法(在blablaViewController.m - 名称取决于项目的名称):Apple OpenGl ES中的显示链接和静态变量魔术样本
//sets up a CADisplayLink to do a regular (draw & update) call like this
CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self
selector:@selector(drawFrame)];
[aDisplayLink setFrameInterval:animationFrameInterval];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
和并条机方法里面执行以下操作:
//start of method
...
static float transY = 0.0f;
...
//Quite a lot of OpenGl code, I am showing only parts of the OpenGL ES1 version:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
transY += 0.075f;
...
//end of method
我目前还不知道很多Objective C,但是这种transY变量被重置的方式,然后在同一个方法中增加是非常奇怪的。由于GL_MODELVIEW矩阵在被移位之前被重置为身份,我不认为它可以在某处保留opengl中的累积值。
static关键字是这里的诡计吗? Objective C忽略了所有将来的变量声明,一旦声明了静态的一次?
感谢您的帮助!
感谢您的快速回复。 – rdrey 2011-04-22 22:01:25
我会期望这是一个实例变量,但我想这样的方法中包含的方法特定变量也很整洁。 – rdrey 2011-04-22 22:11:21
在一个方法中本地声明静态变量可以用于这样的简单示例和其他特殊情况。但请记住,这意味着变量将存在于整个应用程序生命周期中,并且也将在同一类的所有实例中共享。这并不总是你想要的。 – eugeniodepalo 2011-04-22 22:19:44