旋转后更改国际象棋场的颜色

时间:2019-03-27 11:17:29

标签: ios objective-c nsarray subview

我是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];
        }
    }
}

我的任务是旋转时更改字段的颜色,例如,在向左旋转时,用青色和紫色填充该字段。不清楚如何去做。

2 个答案:

答案 0 :(得分:1)

@Vlad Bataev检查我的更新代码:

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中视图的名称以及在rect中将subviews添加为anotherRect的位置来更改视图的名称。 < / p>

如有任何问题,请随时问我。快乐编码:)

答案 1 :(得分: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];
    }
}

}