iOS:如何在 UIView 上添加投影和描边阴影?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17502082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:15:37  来源:igfitidea点击:

iOS : How to add drop shadow and stroke shadow on UIView?

iosobjective-cuiviewdropshadowstrokeshadow

提问by Hemang

I want to add drop shadowand stroke shadowon UIViewThis is what my designer given me to apply shadow,

我要添加阴影中风的影子UIView这是我的设计师给我申请阴影,

  • For drop shadow, he told me to use RGB(176,199,226) with 90% opacity, distance-3 and size-5

  • For stroke shadow, he told to apply, RGB(209,217,226) of size-1 and 100% opacity.

  • 对于阴影,他告诉我使用 RGB(176,199,226),不透明度为 90%,距离为 3,尺寸为 5

  • 对于描边阴影,他告诉应用大小为 1 的 RGB(209,217,226) 和 100% 不透明度。

This is photoshop applied effects screen,

这是Photoshop应用效果屏幕,

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

The view with the required shadow (expected output)

具有所需阴影的视图(预期输出)

enter image description here

在此处输入图片说明

I tried the following to get the drop shadow

我尝试了以下方法来获得阴影

viewCheck.layer.masksToBounds = NO;
viewCheck.layer.cornerRadius = 5.f;
viewCheck.layer.shadowOffset = CGSizeMake(.0f,2.5f);
viewCheck.layer.shadowRadius = 1.5f;
viewCheck.layer.shadowOpacity = .9f;
viewCheck.layer.shadowColor = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowPath = [UIBezierPath bezierPathWithRect:viewCheck.bounds].CGPath;

And this is the result of it,

这就是它的结果,

enter image description here

在此处输入图片说明

I'm having problem in understanding how I can apply stroke and drop shadow as showing into photoshop screenshots (I've added above). How to apply Distance, Spread, Size, Position?

我在理解如何将笔触和阴影应用到 Photoshop 屏幕截图中时遇到问题(我已在上面添加)。如何应用距离、传播、大小、位置?

Can someone point me to a guide to applying these kind of shadows? There's lots of shadow effects coming out and I want to learn how it can be possible instead asking each of the questions here!

有人可以指点我应用这些阴影的指南吗?有很多阴影效果出来了,我想了解它是如何实现的,而不是在这里问每个问题!

Thanks :)

谢谢 :)

回答by Morse

I believe most of configuration you look for can be achieved by employing the shadowPathproperty.

我相信您寻找的大部分配置都可以通过使用该shadowPath属性来实现。

viewCheck.layer.shadowRadius  = 1.5f;
viewCheck.layer.shadowColor   = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowOffset  = CGSizeMake(0.0f, 0.0f);
viewCheck.layer.shadowOpacity = 0.9f;
viewCheck.layer.masksToBounds = NO;

UIEdgeInsets shadowInsets     = UIEdgeInsetsMake(0, 0, -1.5f, 0);
UIBezierPath *shadowPath      = [UIBezierPath bezierPathWithRect:UIEdgeInsetsInsetRect(viewCheck.bounds, shadowInsets)];
viewCheck.layer.shadowPath    = shadowPath.CGPath;

e.g, by setting shadowInsetsthis way

例如,通过设置shadowInsets这样

UIEdgeInsets shadowInsets = UIEdgeInsetsMake(0, 0, -1.5f, 0);

you will get a nice desirable shadow:

你会得到一个很好的理想阴影:

enter image description here

在此处输入图片说明

You can decide now how you want the shadow rectangle to be constructed by controlling the shadow path rectangle insets.

您现在可以通过控制阴影路径矩形插入来决定如何构建阴影矩形。

回答by Nimit Parekh

Following are the steps for User Defined Runtime Attribute.

以下是 的步骤 User Defined Runtime Attribute

  1. Open a storyboard or xib file in Interface Builder.
  2. In Interface Builder, select the object to add the attribute to.
  3. Choose View > Utilities > Show Identity Inspector.
  1. 在 Interface Builder 中打开故事板或 xib 文件。
  2. 在 Interface Builder 中,选择要向其添加属性的对象。
  3. 选择“视图”>“实用程序”>“显示身份检查器”。

The Identity inspector appears in the utility area. As shown below, the user defined runtime attributes editor is one of the items in the inspector.

身份检查器出现在实用程序区域中。如下所示,用户定义的运行时属性编辑器是检查器中的项目之一。

enter image description here

在此处输入图片说明

  1. Click the Add button (+) in the lower left of the user defined runtime attributes editor.
  1. 单击用户定义的运行时属性编辑器左下方的添加按钮 (+)。

enter image description here

在此处输入图片说明

回答by iPatel

For give BorderOf UIView.

对于给定边界UIView

Add #import "QuartzCore/QuartzCore.h"fram work.

添加#import "QuartzCore/QuartzCore.h"框架工作。

myView.layer.cornerRadius = 15.0; // set as you want.
myView.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want.
myView.layer.borderWidth = 1.0; // set as you want.

回答by Esqarrouth

I am sorry, I only have the Swift solution but heres my UIView extensions which I use to do this task:

抱歉,我只有 Swift 解决方案,但这是我用来执行此任务的 UIView 扩展:

// MARK: Layer Extensions
extension UIView {

    func setCornerRadius(#radius: CGFloat) {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }

    func addShadow(#offset: CGSize, radius: CGFloat, color: UIColor, opacity: Float, cornerRadius: CGFloat? = nil) {
        self.layer.shadowOffset = offset
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = opacity
        self.layer.shadowColor = color.CGColor
        if let r = cornerRadius {
            self.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: r).CGPath
        }
    }

    func addBorder(#width: CGFloat, color: UIColor) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.CGColor
        self.layer.masksToBounds = true
    }

    func drawStroke(#width: CGFloat, color: UIColor) {
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.w, height: self.w), cornerRadius: self.w/2)
        let shapeLayer = CAShapeLayer ()
        shapeLayer.path = path.CGPath
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.strokeColor = color.CGColor
        shapeLayer.lineWidth = width
        self.layer.addSublayer(shapeLayer)
    }

}

I never tried it but you could just paste this code to any Swift file and probably call them from Obj-C code.

我从未尝试过,但您可以将此代码粘贴到任何 Swift 文件中,并可能从 Obj-C 代码中调用它们。