【问题标题】:Change color of chess field after rotate旋转后改变棋盘颜色
【发布时间】:2019-08-17 22:39:09
【问题描述】:

我是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


    【解决方案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 click 上执行了颜色更改事件,因此您必须在旋转事件后使用该代码。

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

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

    【讨论】:

    • 效果很好!谢谢!我才刚刚开始调查 IBActions。我在你的代码中对白色做了一些小改动,现在它变成了紫色:) 谢谢!
    • 太棒了,喜欢在 obj c 中工作。当您必须实现任何操作属性(例如按钮、段、开关)时,您可以使用 IBAction。您也可以以编程方式执行此操作。谢谢
    • 感谢您,也祝您编码愉快:)
    【解决方案2】:

    寻找另一种可能的解决方案来改变颜色:

    -(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];
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 2017-01-30
      • 2015-11-07
      • 1970-01-01
      • 2018-09-25
      • 2017-12-13
      • 2023-04-10
      • 2012-08-17
      相关资源
      最近更新 更多