简体   繁体   English

UIView Shadow Gradient

[英]UIView Shadow Gradient

I have created a custom UIView in my iOS project that has a drop shadow. 我在我的iOS项目中创建了一个自定义UIView,它有一个投影。 My aim is to apply the same gradient to the shadow as it is on the view's background. 我的目标是将相同的渐变应用于阴影,就像在视图的背景上一样。

Below is an example of how my current solid colour shadows look. 下面是我当前纯色阴影的外观示例。

影子示例 This is done through a subclass of UIView with the below code: 这是通过UIView的子类完成的,代码如下:

override func layoutSubviews() {
    let gradientLayer = layer as! CAGradientLayer
    gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
    gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
    gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
    layer.cornerRadius = cornerRadius
    layer.shadowColor = shadowColor.cgColor
    layer.shadowOffset = CGSize(width: shadowX, height: shadowY)
    layer.shadowRadius = shadowBlur
    layer.shadowOpacity = 1

    let inset: CGFloat = bounds.width * 0.05
    layer.shadowPath = UIBezierPath(roundedRect: bounds.insetBy(dx: inset, dy: 0.0), cornerRadius: cornerRadius).cgPath
}

I have been playing around with creating a second gradient layer and masking it to the shadow but have had no luck. 我一直在玩创建第二个渐变层并将其遮盖到阴影中,但没有运气。 Please point me in the right direction! 请指出我正确的方向!

I think you can't do more with standard CALayer shadow. 我认为你不能用标准的CALayer阴影做更多的事情。 Take a look at filters property of CALayer. 看看CALayer的过滤器属性。

https://developer.apple.com/documentation/quartzcore/calayer/1410901-filters https://developer.apple.com/documentation/quartzcore/calayer/1410901-filters

Create second CAGradientLayer and apply a GausianBlur filter for example. 例如,创建第二个CAGradientLayer并应用GausianBlur过滤器。

https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur

Disable standard layer shadows and add your blurred layer as sublayer. 禁用标准图层阴影并将模糊图层添加为子图层。

I believe @jacek is right, you're pushing the limit of what you can do with CALayer shadows (which isn't much to be honest) 我相信@jacek是对的,你正在推动你可以用CALayer阴影做的限制(这不是很诚实)

The best things to do as jacek explain is to create your own shadow, though another CAGradientLayer applying Gaussian blur to act as a shadow. 像jacek解释的最好的事情是创建自己的阴影,尽管另一个CAGradientLayer应用高斯模糊作为阴影。

Also, I don't believe placing your code inside the layoutSubviews is the most appropriate place. 另外,我不认为将您的代码放在layoutSubviews是最合适的地方。 Once configured your layer won't need to change much, and thus it will be called a lot if your layout change a lot. 一旦配置,您的图层将不需要进行太多更改,因此如果您的布局发生很大变化,它将被调用很多。

Usually if using a view coming from interface-builder, I would call this logic from awakeFromNib inside a dedicated configureBackgroundViews function for example. 通常,如果使用来自interface-builder的视图,我会在专用的configureBackgroundViews函数中从awakeFromNib调用此逻辑。

Use my code for your requirement available in my gitRepo https://github.com/Krishnarjun-Banoth/BlurView/tree/master/Shadows 根据我的gitRepo中提供的要求使用我的代码https://github.com/Krishnarjun-Banoth/BlurView/tree/master/Shadows

Step1: Just drag and drop BlurEffect.swift file in your project. 第1步:只需在项目中拖放BlurEffect.swift文件即可。

Step2: Then simply Use below View extension methods for each one your views individually. 第2步:然后只需单独使用下面的查看扩展方法为您的每个视图单独。

  testView.applyGradient(colours: [UIColor.blue,UIColor.orange]) //pass your required colours.
  testView.blur(blurRadius: 5.0)

Note: Inspired from @Jacek Głazik answer and FlexMonkey's tutorial. 注意:灵感来自@JacekGłazik的回答和FlexMonkey的教程。

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

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