如何使用渐变UIButton创建光泽

时间:2013-04-05 10:13:50

标签: iphone ios objective-c ipad uibutton

创建像这样的按钮的最简单方法是什么,而不必使用.png并将其设置为背景?

enter image description here

2 个答案:

答案 0 :(得分:2)

 CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.rem.bounds;

    gradient.cornerRadius=7.0f;

    gradient.colors = [NSArray arrayWithObjects:
                       (id)[[UIColor blueColor] CGColor],
                       (id)[[UIColor blueColor] CGColor], (id)[[UIColor whiteColor] CGColor],nil];
    gradient.opacity=0.7f;

    [self.rem.layer insertSublayer:gradient atIndex:0];
    self.rem.layer.cornerRadius=7.0f;
    [self.rem.layer setBorderWidth:0.50f];

试试这个和import QuartzCore

rem是UIButton outlet

我希望这能解决你的问题

答案 1 :(得分:0)

这是Gradient按钮的常用方法。

CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = btn.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
                              (id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],
                              (id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],
                             nil];
[btn.layer insertSublayer:btnGradient atIndex:0];