简体   繁体   English

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

[英]How to do colour shading in Objective-C

I need to do a colour shading in objective c. 我需要在目标c中进行颜色着色。 As shown in this image shade is on the button of HardService and SoftService. 如图所示,阴影位于HardService和SoftService的按钮上。

Is there is any way to do this ? 有没有办法做到这一点? Thanks in Advance! 提前致谢!

在此输入图像描述

CAGradientLayer would server your purpose - Reference: CAGradientLayer将服务于您的目的 - 参考:

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

You can use either a gradient image or CAGradientLayer for this purpose 您可以使用渐变图像或CAGradientLayer来实现此目的

CAGradientLayer added to an existing button: 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];

You can use an image for showing shadow. 您可以使用图像显示阴影。 Use blur image or change alpha of the imageview. 使用模糊图像或更改图像视图的alpha。

Or if you want to do it programmatically, try it: 或者如果您想以编程方式执行此操作,请尝试:

Obj 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;

Swift : 斯威夫特:

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

Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow? 取自http://www.innofied.com/implementing-shadow-ios/,并且还要了解更多: UIView有圆角和阴影?

My another answer related to this: Drop shadow in ios 我的另一个答案与此相关: 在ios中投下阴影

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM