Objective c 为什么边框厚度会随着按钮的颜色而变化(蓝色的边框为2倍)?

Objective c 为什么边框厚度会随着按钮的颜色而变化(蓝色的边框为2倍)?,objective-c,ios,uibutton,border,Objective C,Ios,Uibutton,Border,我正在做一个游戏,由于某种原因,边框的厚度会根据按钮的颜色而变化。这个问题直到我在应用程序上放了一个背景图片才发生,所以我不确定是什么原因造成的。我拍了几张截图来说明我的意思。由于某些原因,当边框按钮为蓝色时,边框厚度将更改为2px而不是1px 此外,控制按钮上的边框也存在一些问题。我所说的控制按钮是指顶部的6个按钮,它们的顺序是红色、蓝色、棕色、绿色、紫色和黄色 以下是截图: 我通过在背景中放置一个按钮来创建边框,该按钮从x-1,y-1开始,宽度为+2,高度为+2。下面是一段代码,让您了解:

我正在做一个游戏,由于某种原因,边框的厚度会根据按钮的颜色而变化。这个问题直到我在应用程序上放了一个背景图片才发生,所以我不确定是什么原因造成的。我拍了几张截图来说明我的意思。由于某些原因,当边框按钮为蓝色时,边框厚度将更改为2px而不是1px

此外,控制按钮上的边框也存在一些问题。我所说的控制按钮是指顶部的6个按钮,它们的顺序是红色、蓝色、棕色、绿色、紫色和黄色

以下是截图:

我通过在背景中放置一个按钮来创建边框,该按钮从x-1,y-1开始,宽度为+2,高度为+2。下面是一段代码,让您了解:

 UIButton *borderButton = [UIButton buttonWithType:UIButtonTypeCustom];
 borderButton.frame = CGRectMake((controlX-1), (controlY-1), 42, 32);
 borderButton.backgroundColor = [UIColor blackColor];
 [self.view addSubview:borderButton];

 UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [controlButton addTarget:self
                      action:@selector(doMove:)
            forControlEvents:UIControlEventTouchDown];
 controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
 controlButton.tag = 500 + i;
 tmpColor = [self.colors objectAtIndex:i];
 controlButton.backgroundColor = tmpColor;
 [self.view addSubview:controlButton];
此代码取自一个循环,其中controlX和controlY给出了控制按钮启动位置的x/y值


如果有人知道如何解决这个问题,我将不胜感激。如果我以错误的方式处理边界,并且有一种更简单、问题更少的方法来实现相同的外观,我也愿意这样做,因为在我这方面不会有太多代码需要更改。提前感谢所有能帮我解决这个恼人的UI问题的人。

实现带边框自定义按钮的最简单方法是使用按钮的图层:

#import <QuartzCore/QuartzCore.h>
...
UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [controlButton addTarget:self
                      action:@selector(doMove:)
            forControlEvents:UIControlEventTouchDown];
 controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
 controlButton.tag = 500 + i;

 // Set layer properties
 controlButton.layer.borderWidth = 1.0;
 controlButton.layer.borderColor = [UIColor blackColor].CGColor;
 controlButton.layer.cornerRadius = 5.0; // if you want rounded corners...

 tmpColor = [self.colors objectAtIndex:i];
 controlButton.backgroundColor = tmpColor;
 [self.view addSubview:controlButton];
#导入
...
UIButton*controlButton=[UIButton按钮类型:UIButtonTypeCustom];
[controlButton添加目标:自我
操作:@selector(doMove:)
forControlEvents:UIControlEventTouchDown];
controlButton.frame=CGRectMake(controlX,controlY,40,30);
controlButton.tag=500+i;
//设置图层特性
controlButton.layer.borderWidth=1.0;
controlButton.layer.borderColor=[UIColor blackColor].CGColor;
controlButton.layer.cornerRadius=5.0;//如果你想要圆角。。。
tmpColor=[self.colors对象索引:i];
controlButton.backgroundColor=tmpColor;
[self.view addSubview:controlButton];
您可能还需要检查按钮和其他元素(如标签)是否位于积分坐标处,以避免图像模糊。要更正坐标,请使用


另外,对于复杂的动画/游戏逻辑,最好使用类似于

的框架。实现带有边框的自定义按钮的最简单方法是使用按钮的层:

#import <QuartzCore/QuartzCore.h>
...
UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [controlButton addTarget:self
                      action:@selector(doMove:)
            forControlEvents:UIControlEventTouchDown];
 controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
 controlButton.tag = 500 + i;

 // Set layer properties
 controlButton.layer.borderWidth = 1.0;
 controlButton.layer.borderColor = [UIColor blackColor].CGColor;
 controlButton.layer.cornerRadius = 5.0; // if you want rounded corners...

 tmpColor = [self.colors objectAtIndex:i];
 controlButton.backgroundColor = tmpColor;
 [self.view addSubview:controlButton];
#导入
...
UIButton*controlButton=[UIButton按钮类型:UIButtonTypeCustom];
[controlButton添加目标:自我
操作:@selector(doMove:)
forControlEvents:UIControlEventTouchDown];
controlButton.frame=CGRectMake(controlX,controlY,40,30);
controlButton.tag=500+i;
//设置图层特性
controlButton.layer.borderWidth=1.0;
controlButton.layer.borderColor=[UIColor blackColor].CGColor;
controlButton.layer.cornerRadius=5.0;//如果你想要圆角。。。
tmpColor=[self.colors对象索引:i];
controlButton.backgroundColor=tmpColor;
[self.view addSubview:controlButton];
您可能还需要检查按钮和其他元素(如标签)是否位于积分坐标处,以避免图像模糊。要更正坐标,请使用


另外,对于复杂的动画/游戏逻辑,最好使用类似于

的框架。问题仍然存在,按钮上的边框看起来不清晰。这是我实现了你的代码后的一个屏幕截图:你看到蓝色的那个看起来有多奇怪了吗?红色的也一样,但蓝色是最明显的。iOS中蓝色按钮的边框是否存在固有问题?谢谢你的建议,谢谢你的回复,因为这是一个比我做的更好的处理边界的方法。啊,我明白你的意思。你可以玩borderWidth/cornerRadius(增加borderWidth?)。但最好的选择是使用自定义背景图像(或使用)。感谢您的帮助,我尝试了一下,我发现最好的解决方案就是更改按钮的颜色。我把它们做得轻了一点,现在问题不那么明显了。问题仍然存在,按钮上的边框看起来有点不对劲。这是我实现了你的代码后的一个屏幕截图:你看到蓝色的那个看起来有多奇怪了吗?红色的也一样,但蓝色是最明显的。iOS中蓝色按钮的边框是否存在固有问题?谢谢你的建议,谢谢你的回复,因为这是一个比我做的更好的处理边界的方法。啊,我明白你的意思。你可以玩borderWidth/cornerRadius(增加borderWidth?)。但最好的选择是使用自定义背景图像(或使用)。感谢您的帮助,我尝试了一下,我发现最好的解决方案就是更改按钮的颜色。我把它们调轻了一点,现在问题不那么明显了。