旋转后改变国际象棋领域的颜色

问题描述 投票:1回答:2

我在Obj-C的新手。我有以下国际象棋领域:

for (int row = 0; row < 8; row++) {
    for (int column = 0; column < 8; column++) {

        CGRect square = {horizontalOffSet + (column * squareSize),
                         verticalOffSet + (row * squareSize),
                         squareSize, squareSize};
        UIView *rect = [[UIView alloc] initWithFrame:square];
        rect.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

        horizontalOffSet = horizontalOffSet + squareSize;
        if ((row + column) % 2 == 0) {
            //verticalOffSet = verticalOffSet + squareSize;
            horizontalOffSet = 0;
            rect.backgroundColor = [UIColor whiteColor];
            [anotherRect addSubview:rect];
        } else {
            //verticalOffSet = verticalOffSet + squareSize;
            horizontalOffSet = 0;
            rect.backgroundColor = [UIColor blackColor];
            [anotherRect addSubview:rect];
        }
    }
}

我的任务是在旋转时改变场的颜色,例如,当向左旋转时,用蓝色和紫色填充它。不清楚如何做到这一点。

ios objective-c nsarray subview
2个回答
1
投票

@somerk查看我更新的代码:

1)我给识别的国际象棋视图提供了标识符(tag),它有白色和黑色。你可以没有标识符,但在多个subviews标识符的情况下是更好的方法获得。

 int horizontalOffSet = 2;
    int squareSize = 40;
    int verticalOffSet = 2;

    for (int row = 0; row < 8; row++) {
        for (int column = 0; column < 8; column++) {

            CGRect square = {horizontalOffSet + (column * squareSize),
                verticalOffSet + (row * squareSize),
                squareSize, squareSize};
            UIView *rect = [[UIView alloc] initWithFrame:square];
            rect.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

            horizontalOffSet = horizontalOffSet + squareSize;
            if ((row + column) % 2 == 0) {
                //verticalOffSet = verticalOffSet + squareSize;
                horizontalOffSet = 0;
                rect.tag = 101;
                rect.backgroundColor = [UIColor whiteColor];
                [self.view addSubview:rect];
            } else {
                //verticalOffSet = verticalOffSet + squareSize;
                rect.tag = 102;
                horizontalOffSet = 0;
                rect.backgroundColor = [UIColor blackColor];
                [self.view addSubview:rect];
            }
        }

2)我在UIButton上执行了颜色更改事件,因此您必须在旋转事件后使用该代码。

-(IBAction)changeColor:(UIButton *)btn{
    for(UIView *rectViews in self.view.subviews){
        UIColor *clrblck = [UIColor blackColor];
        if(rectViews.backgroundColor == clrblck && rectViews.tag == 102){
            rectViews.backgroundColor = [UIColor cyanColor];
        }else if(rectViews.backgroundColor == clrblck && rectViews.tag == 101){
            rectViews.backgroundColor = [UIColor purpleColor];
        }else{
             // you will get other views here..
        }
    }
}
  • 我已经获得所有子视图并检查backgroundColortag并更改其backgroundColor

注意:根据您在loop中的视图名称更改view'name,并在rect中将subviews添加为anotherRect

如果您有任何疑问,可以随时问我。快乐编码:)


0
投票

找到另一种可能的方法来改变颜色:

-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

CGFloat rndmColor1 = (float)(arc4random() % 256) / 255;
CGFloat rndmColor2 = (float)(arc4random() % 256) / 255;


for(UIView *rect in self.chessField.subviews){
    if (rect.tag == 102) {
        rect.backgroundColor = [UIColor colorWithHue:rndmColor1 saturation:1 brightness:1 alpha:1];
    } else if (rect.tag == 101) {
        rect.backgroundColor = [UIColor colorWithHue:rndmColor2 saturation:1 brightness:1 alpha:1];
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.