如何在Objective-C中进行颜色着色

时间:2016-10-22 07:08:13

标签: ios objective-c xcode uibutton

我需要在目标c中进行颜色着色。如图所示,阴影位于HardService和SoftService的按钮上。

有没有办法做到这一点?在此先感谢!

enter image description here

3 个答案:

答案 0 :(得分:2)

CAGradientLayer将服务于您的目的 - 参考:

http://blog.apoorvmote.com/gradient-background-uiview-ios-swift/

答案 1 :(得分:1)

您可以使用渐变图像或CAGradientLayer来实现此目的

CAGradientLayer已添加到现有按钮:

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

答案 2 :(得分:0)

您可以使用图像显示阴影。使用模糊图像或更改图像视图的alpha。

或者,如果您想以编程方式执行此操作,请尝试:

对象:

//create elliptical shadow for button through UIBezierPath
CGRect ovalRect = button.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];

 //applying shadow to path
 button.layer.shadowColor = [UIColor yourcolor].CGColor;
 button.layer.shadowOffset = CGSizeMake(0.0, 0.0);
 button.layer.shadowOpacity = 1.0;
 button.layer.shadowRadius = 3.0;
 button.layer.shadowPath = path.CGPath;
斯威夫特:

//create elliptical shdow forimage through UIBezierPath
var ovalRect = button.bounds
var path = UIBezierPath(ovalInRect: ovalRect)

//applying shadow to path
button.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor
 button.layer.shadowOffset = CGSizeMake(0.0, 0.0)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 3.0
button.layer.shadowPath = path.CGPath

取自http://www.innofied.com/implementing-shadow-ios/,并且还要了解更多信息:UIView with rounded corners and drop shadow?

我的另一个答案与此相关:Drop shadow in ios