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

发布于 2024-09-19 09:22:57 字数 137 浏览 4 评论 0原文

我想为某些 iPhone UIButtons 添加 3D 或光泽外观,但这样做时不使用 PNG 或可拉伸图像。我有很多不同形状和大小的按钮,其中有很多颜色是动态生成的,因此预渲染图像在我的情况下不可行。

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

I would like to add 3D or glossy look to some iPhone UIButtons, but do so without the use of PNGs or stretchable images. I have a lot of buttons of varying shapes and sizes with lots of colors which are generated on the fly, so prerendered images are not feasible in my case.

How would you go about drawing the gloss on these buttons programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-09-26 09:22:57

这个答案中,我提供了一些用于绘图的代码UIView 上的光泽渐变,但是 Mirko 的答案对于同一个问题,显示了一种更高效的方法。

我创建了一个更精致的 3D 按钮样式,带有光泽和阴影,作为我的课程的示例。该 3-D 按钮的代码是软件包的一部分,可以在 此处下载 (查找 MATCGlossyButton UIButton 子类)。我在 iTunes U

In this answer, I provide some code for drawing a gloss gradient over a UIView, but Mirko's answer to that same question shows a more performant way of doing this.

I created a more elaborate 3-D button style, with gloss and shadowing, as an example for my course. The code for that 3-D button is part of the package that can be downloaded here (look for the MATCGlossyButton UIButton subclass). I describe the drawing of this element in the video for the Quartz 2D class from the spring semester of my iPhone development course on iTunes U.

三生路 2024-09-26 09:22:57

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

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

One way to do this programatically is to modify properties of a UIButton's layer. For example, you can add an instance of CAGradientLayer as a sublayer, set the layer's corner radius, modify it's border, etc. Matt Long has written a great article about this:

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

黑凤梨 2024-09-26 09:22:57
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];
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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文