Apple OpenGl ES Sample中的CADisplayLink和静态变量magic

| 我想解释一下为什么XCode的OpenGl ES Sample有效。它执行以下操作以启动drawFrame方法(在blablaViewController.m中-名称取决于项目的名称):
//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];
在drawFrame方法中,它执行以下操作:
//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中的某个位置。 静态关键字是诀窍吗?一旦将某事物声明为静态,Objective C是否会忽略所有将来的变量声明? 谢谢您的帮助!     
已邀请:
静态变量在编译时以二进制形式进行初始化,因此只能进行一次初始化,因此,您被禁止为初始化分配动态值。此处,变量
transY
不会在每次方法调用时都设置为0.0,而只是在启动时设置。这就是为什么该方法的后续调用可以检索旧值的原因。     

要回复问题请先登录注册