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

Posted

技术标签:

【中文标题】如何在 Objective-C 中进行颜色着色【英文标题】:How to do colour shading in Objective-C 【发布时间】:2016-10-22 07:08:13 【问题描述】:

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

有没有办法做到这一点?提前致谢!

【问题讨论】:

您可以为此使用渐变图像或 CAGradientLayer 【参考方案1】:

CAGradientLayer 将满足您的目的 - 参考:

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

【讨论】:

【参考方案2】:

您可以为此使用渐变图像或 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];

【讨论】:

【参考方案3】:

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

或者,如果您想以编程方式进行,请尝试:

对象c:

//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

【讨论】:

kShadowColor 听到了什么? 我想要按钮上的阴影而不是按钮的阴影。

以上是关于如何在 Objective-C 中进行颜色着色的主要内容,如果未能解决你的问题,请参考以下文章

SceneKit - 在着色器修改器中访问目标颜色以进行混合

如何在没有蓝色的所有感觉颜色的情况下将 QUADS 着色为蓝色

我如何在iOS上以编程方式为图像着色?

如何在 Objective-C 中更改垃圾按钮的颜色? [复制]

如何在 Vega-Lite 中用特定颜色为线条着色?

如何在渲染之间更改片段着色器颜色?