Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在目标c中使用For循环?_Objective C - Fatal编程技术网

Objective c 在目标c中使用For循环?

Objective c 在目标c中使用For循环?,objective-c,Objective C,在目标c中,当我按下播放按钮(使用alpha)时,我试图将荧光灯一个接一个地穿过两个平行的盒子 (每个盒子有8张图片的数组) 问题是只有一个方框的图片被突出显示。而另一个仅在第一个完成后高亮显示。我不知道使用for循环是否会导致问题?或者别的什么 - (IBAction)didPressPlay:(id)sender { self.playing = YES; self.timer= [NSTimer scheduledTimerWithTimeInterval:60.0/s

在目标c中,当我按下播放按钮(使用alpha)时,我试图将荧光灯一个接一个地穿过两个平行的盒子 (每个盒子有8张图片的数组)

问题是只有一个方框的图片被突出显示。而另一个仅在第一个完成后高亮显示。我不知道使用for循环是否会导致问题?或者别的什么

 - (IBAction)didPressPlay:(id)sender {

    self.playing = YES;
    self.timer= [NSTimer scheduledTimerWithTimeInterval:60.0/self.BPM target:self selector:@selector(timerFire:) userInfo:nil repeats:YES]; 
 }

 - (void) timerFire:(NSTimer *)timer {

    for (UIButton *button in self.trackOneButtons) {

        if (button.tag == self.sampleNumber) {

            button.alpha=1.0;
        }
        else {
            button.alpha = .5;
        }

        for (UIButton *button in self.trackTwoButtons) {
            if (button.tag == self.sampleNumber) {
                button.alpha=1.0;
            }
            else {
                button.alpha =0.5;
            }
        }
    }

    self.sampleNumber ++;
    if (self.sampleNumber >7) {
        self.sampleNumber = 0;
    }
 }

假设两个数组的计数始终相同,可以尝试将循环更改为:

- (void) timerFire:(NSTimer *)timer {

    CGFloat sampleNum = self.sampleNumber;

    void (^alphaAdjust)(UIView*) = ^void(UIView*button){
        if (button.tag == sampleNum){
            button.alpha=1.0;
        } else {
            button.alpha =.5;
        }
    };

    for (int i = 0; i!=self.trackOneButtons.count; i++) {

        UIView *btn1 = self.trackOneButtons[i];
        alphaAdjust(btn1);
        UIView *btn2 = self.trackTwoButtons[i];
        alphaAdjust(btn2);
    }
...
}

这里有很多未知数,任何人都需要更多的信息来帮助。也就是说,调试任何循环的最佳方法之一是在调试器中逐步完成它。在timerFire的开始处设置一个断点,在观察button.tag的值的同时逐步执行。换句话说,我如何使用两个for循环一起工作(第一个for循环的第一个成员与第二个for循环的第一个成员),然后两个for循环的第二个成员一起工作,依此类推,我认为糟糕的缩进和格式可能是问题的一大部分。如果你解释一下你的两个按钮数组是什么,那会有所帮助。并解释这意味着什么:“问题是只有一个方框的图片被高亮显示,而另一个方框的图片只有在第一个方框完成后才被高亮显示。”