Iphone 从另一个具有CADisplayLink的方法调用一次该方法

Iphone 从另一个具有CADisplayLink的方法调用一次该方法,iphone,xcode,frame,boolean,cadisplaylink,Iphone,Xcode,Frame,Boolean,Cadisplaylink,我想从方法“method1”调用另一个方法“method2”。问题是“method1”上有一个Cadisplay链接,当我想从“method1”调用“method2”时,它会以每秒60次的速度调用它,所以我只想让它调用一次。我知道我必须使用BOOL变量,但我不知道如何使用它们。有人能帮我吗?对不起,我说的是英语,我是法国人:/ //编辑: method1上有一个Cadisplay链接: -(void)method1{ if( if ( leScore % 20000 == 0) { [self

我想从方法“method1”调用另一个方法“method2”。问题是“method1”上有一个Cadisplay链接,当我想从“method1”调用“method2”时,它会以每秒60次的速度调用它,所以我只想让它调用一次。我知道我必须使用BOOL变量,但我不知道如何使用它们。有人能帮我吗?对不起,我说的是英语,我是法国人:/

//编辑: method1上有一个Cadisplay链接:

-(void)method1{
if(
if ( leScore % 20000 == 0) {
[self method2];
}


因此,每次'leScore%20000==0'调用一次方法2。

如果您想使方法调用只发生一次,请按以下方式使用bool:

@interface SomeClass {
    BOOL method2RunFlag; // set to NO in init
}
@end

// ... in your method1

if( method2RunFlag == NO ) {
    // call your method2
    method2RunFlag = YES;
}
根据上面编辑的代码:

-(void)method1{
if( method2RunFlag == NO ) {
method2RunFlag = YES;
  if ( leScore % 20000 == 0) {
    [self method2];
  }
    // wait 1 second before able to call again
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(resetMethod2RunFlag:) userInfo:nil repeats:NO];
}
- (void)resetMethod2RunFlag:(NSTimer *)timer {
  method2RunFlag = NO;
}

仍然不能完全确定你在追求什么,但这是我最好的猜测。=)

您可能希望创建method1的两个变体,一个用于CADisplayLink,另一个用于其他地方,可能会调用helper method1A中的所有公共代码,但带有一个标志参数,表示是否调用method2。

不,一次不调用,因为我想下次调用它。以你的方式,我不能再叫它了:/如果你想以后再叫它,你可以把BOOL设置为NO,这样就可以再次运行它。或者我不明白你到底想做什么?但是我怎么能把它设为“否”。因为我在方法中将它设为“是”。在将其设置为NOA后,我是否必须在方法中将其设置为“是”。另一种选择是使用计数器。将BOOL替换为每次调用method1时递增的整数值。然后你可以检查它是否超过(x)值,如果超过,则运行method2,将计数器重置为零,然后等待再次调用。嗯,这对我来说有点难,你能用代码解释一下吗?
-(void)method1{
if( method2RunFlag == NO ) {
method2RunFlag = YES;
  if ( leScore % 20000 == 0) {
    [self method2];
  }
    // wait 1 second before able to call again
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(resetMethod2RunFlag:) userInfo:nil repeats:NO];
}
- (void)resetMethod2RunFlag:(NSTimer *)timer {
  method2RunFlag = NO;
}