2012-12-10 91 views
1

我正在做一个游戏,并且由于某种原因,边框厚度根据按钮的颜色而改变。直到我在应用程序上放置背景图像时才发生这个问题,所以我不确定是什么原因造成的。我拿了几张截图来展示我的意思。出于某种原因,当边界按钮为蓝色时,边框厚度变为2px而不是1px。为什么边框厚度会随着按钮颜色(蓝色获得2px边框)而改变?

另外,控制按钮边框也有一些问题。通过控制按钮,我指的是顶部的6个按钮,顺序为红色,蓝色,棕色,绿色,紫色和黄色。

下面是截图:

Screenshot #1Screenshot #2

我通过把一个按钮,其中开始于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故障的人。

回答

1

实现与边境自定义按钮的最简单方法是使用按钮的层:

#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

P.S.对于复杂的动画/游戏逻辑,最好使用如Cocos2D

+0

这样的框架问题仍然存在,即边框在按钮上向外看。这是我实现你的代码后的屏幕截图:http://mdl.fm/f/b3.png你看蓝色的那个看起来很奇怪吗?红色的也是,但蓝色是最引人注目的。 iOS中的蓝色按钮边框有固有的问题吗?感谢您的建议,但您的回复为+1,因为处理边界的方式比我所做的要好得多。 – SISYN

+0

啊,我明白你的意思了。你可以玩borderWidth/cornerRadius(增加borderWidth?)。但最好的选择是使用自定义背景图像(或使用[可伸缩图像](http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/) occ/instm/UIImage/resizableImageWithCapInsets :)) –

+0

感谢您的帮助,我随身携带它,我发现最好的解决方案只是改变按钮的颜色。我让它们变得更轻一点,现在的问题几乎没有那么明显。 – SISYN