如何以编程方式为 UIButton 提供光泽外观?
我想为某些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 这个答案中,我提供了一些用于绘图的代码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.
以编程方式执行此操作的一种方法是修改
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 ofCAGradientLayer
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/