【问题标题】:call a method one time from another method which have a CADisplayLink on it从另一个有 CADisplayLink 的方法调用一个方法
【发布时间】:2011-11-11 20:16:04
【问题描述】:

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

//编辑: 方法1上有一个CADisplayLink:

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

}

-(void)method2{

etatJeu = arc4random() / (UINT_MAX/3);

switch(etatJeu) {
    case 0: /* top */
        etatJeu=kEtatJeu2;
        break;
    case 1: /* bottom */
        etatJeu=kEtatJeu3;              
        break;
    case 2: /* bottom */
        etatJeu=kEtatJeu4;              
        break;
    default:
        break;


}

所以每次 'leScore % 20000 == 0' 调用一次方法 2。

【问题讨论】:

    标签: iphone xcode frame boolean cadisplaylink


    【解决方案1】:

    如果你想让方法调用只发生一次,那么用这种方式使用 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;
    }
    

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

    【讨论】:

    • 不是一次,因为我想再叫一次。以你的方式,我不能再叫它了:/
    • 如果您想稍后再次调用它,您可以将 BOOL 设置为 NO,这将允许它再次运行。或者我不明白你到底想做什么?
    • 但是如何设置为NO。因为我在方法中设置为YES。设置为NO后是否必须在方法中将其设置为YES
    • 另一种方法是使用计数器。将 BOOL 替换为每次调用 method1 时递增的整数值。然后你可以检查它是否超过(x)值,如果是,运行method2,将计数器重置为零,然后等待它再次被调用。
    • 嗯,这对我来说有点难,你能用代码解释一下吗?
    【解决方案2】:

    您可能想要创建方法 1 的 2 个变体,一个用于 CADisplayLink,另一个用于其他地方,可能调用辅助方法 1A 中的所有公共代码,但带有一个标志参数说明是否调用方法 2。

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 2010-12-24
      • 2011-01-14
      相关资源
      最近更新 更多