0

配列に 6 つの uibutton があります。そして、交差せずにランダムな位置を設定したい。私はいくつかのことを試しましたが、私のプロジェクトはうまくいきます。しかし、アプリに新しいボタンを強制的に生成させると、エラーなしでスタックします。

for (UIButton *button in arr) {

    float widthOffset = self.gameView.frame.size.width-button.frame.size.width;
    float heightOffset = self.gameView.frame.size.height-button.frame.size.height;

    button.frame = CGRectMake(arc4random()%(int)widthOffset, arc4random()%(int)heightOffset, button.frame.size.width, button.frame.size.height);

    while ([self button:button intersectsButtonInArray:arr]) {
        button.frame = CGRectMake(arc4random()%(int)widthOffset, arc4random()%(int)heightOffset, button.frame.size.width, button.frame.size.height);
    }
}


-(BOOL)button:(UIButton *)button intersectsButtonInArray:(NSArray *)array {
    for (UIButton *but in array) {
        if (CGRectIntersectsRect(button.frame, but.frame) && ![button isEqual:but]) {
            return YES;
        }
    }
    return NO;
}

私の意見では、問題は while 部分にあります。しかし、私は理解できません。

アドバイスをいただければ幸いです。

よろしく、
タハ

4

0 に答える 0