如何以编程方式为UIButtons提供光泽外观?

时间:2010-09-14 13:13:17

标签: iphone cocoa-touch core-graphics quartz-graphics quartz-2d

我想为某些iPhone UIButton添加3D或光泽外观,但不使用PNG或可伸缩图像。我有很多不同形状和大小的按钮,有很多颜色可以在运行中生成,所以在我的情况下,预呈现的图像是不可行的。

您将如何以编程方式在这些按钮上绘制光泽?

3 个答案:

答案 0 :(得分:3)

this answer中,我提供了一些用于在UIView上绘制光泽渐变的代码,但同一问题的Mirko's answer显示了一种更高效的方法。

我创建了一个更精致的3D按钮样式,带有光泽和阴影,作为我课程的一个例子。该3-D按钮的代码是可以下载的包的一部分here(查找MATCGlossyButton UIButton子类)。我在iTunes U的iPhone开发课程的春季学期中描述了Quartz 2D类视频中这个元素的绘制。

答案 1 :(得分:1)

以编程方式执行此操作的一种方法是修改UIButton图层的属性。例如,您可以添加CAGradientLayer的实例作为子图层,设置图层的角半径,修改它的边框等.Matt Long撰写了一篇很棒的文章:

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

答案 2 :(得分:-1)

UIButton *button1                 =   [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];
button1.backgroundColor           =   [UIColor blackColor];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button1.bounds;
gradient.colors = [NSArray arrayWithObjects: (id)[[UIColor grayColor] CGColor],(id)[[UIColor blackColor] CGColor], nil];
[button1.layer insertSublayer:gradient atIndex:0];