简体   繁体   English

为什么边框的粗细会随着按钮的颜色而变化(蓝色变成2px边框)?

[英]Why does border thickness change with color of button (blue gets 2px border)?

I'm working on a game, and for some reason the border thickness changes depending on what color the button is. 我正在开发游戏,由于某种原因,边框的粗细会根据按钮的颜色而改变。 This problem didn't happen until I put a background image on the app, so I'm not sure what's causing it. 直到我在应用程序上放置了背景图片,这个问题才发生,所以我不确定是什么原因造成的。 I took a couple screenshots to show what I mean. 我拍了几个屏幕截图以显示我的意思。 For some reason, the border thickness changes to 2px instead of 1px when the button that is being bordered is blue. 出于某种原因,当与边框接界的按钮为蓝色时,边框粗细更改为2px而不是1px。

In addition, there are some problems with the borders on the control buttons. 此外,控制按钮上的边框还存在一些问题。 By control buttons, I mean the 6 buttons up top that are in the order of Red, Blue, Brown, Green, Purple, and Yellow. 通过控制按钮,我是指顶部的6个按钮,按红色,蓝色,棕色,绿色,紫色和黄色的顺序排列。

Here are the screenshots: 这是屏幕截图:

截图1截图2

I create the borders by putting a button in the background which starts at x-1,y-1 and has a width of width+2 and height of height+2. 我通过在背景中放置一个按钮来创建边框,该按钮从x-1,y-1开始,宽度为width + 2,高度为height + 2。 Here is a piece of code to give you an idea: 这是一段代码,可以给您一个想法:

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

This code is taken from a loop, where controlX and controlY give x/y values for where the control buttons should start. 此代码从一个循环中获取,其中controlX和controlY给出了控制按钮应在何处开始的x / y值。

If anyone knows how to fix this I would greatly appreciate it. 如果有人知道如何解决此问题,我将不胜感激。 If I am going about borders in the wrong way and there is an easier and less problematic way of achieving the same look, I would be willing to do that as well, because there wouldn't be much code to change on my end. 如果我以错误的方式处理边界,并且有一种更简单且问题较少的方法来获得相同的外观,我也将愿意这样做,因为最终不会有太多代码需要更改。 Thanks in advance to anyone who can help me solve this irritating UI glitch. 在此先感谢任何可以帮助我解决这种令人讨厌的UI故障的人。

The easiest way to implement custom button with border is to use the button's layer: 实现带有边框的自定义按钮的最简单方法是使用按钮的图层:

#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];

You may also need to check that button and other elements (like labels) are located at integral coordinates to avoid blurred images. 您可能还需要检查按钮和其他元素(例如标签)是否位于整数坐标处,以避免图像模糊。 To correct the coordinates use CGRectIntegral 要更正坐标,请使用CGRectIntegral

PS For complex animations/game logic it is better to use frameworks like Cocos2D PS对于复杂的动画/游戏逻辑,最好使用Cocos2D之类的框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM