[英]Why does border thickness change with color of button (blue gets 2px border)?
我正在开发游戏,由于某种原因,边框的粗细会根据按钮的颜色而改变。 直到我在应用程序上放置了背景图片,这个问题才发生,所以我不确定是什么原因造成的。 我拍了几个屏幕截图以显示我的意思。 出于某种原因,当与边框接界的按钮为蓝色时,边框粗细更改为2px而不是1px。
此外,控制按钮上的边框还存在一些问题。 通过控制按钮,我是指顶部的6个按钮,按红色,蓝色,棕色,绿色,紫色和黄色的顺序排列。
这是屏幕截图:
我通过在背景中放置一个按钮来创建边框,该按钮从x-1,y-1开始,宽度为width + 2,高度为height + 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];
您可能还需要检查按钮和其他元素(例如标签)是否位于整数坐标处,以避免图像模糊。 要更正坐标,请使用CGRectIntegral
PS对于复杂的动画/游戏逻辑,最好使用Cocos2D之类的框架
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.