How do I programmatically provide a glossy look to UIButtons?

馋奶兔 提交于 2019-11-30 23:14:54
Brad Larson

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.

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/

Vijay Dhandapani
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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!