Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView Shadow Gradient

I have created a custom UIView in my iOS project that has a drop shadow. 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.

Shadow Example This is done through a subclass of UIView with the below code:

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!

like image 705
calebmanley Avatar asked Sep 27 '17 10:09

calebmanley


People also ask

Can you do a linear-gradient on box shadow?

Now, if we want to add a gradient-y box shadow behind this box, we can do it using a ::before pseudo-element around it and makes it looks like a shadow. As you can tell, since we want a gradient shadow, we're using linear-gradient as the background of the pseudo-element.

How do you make a gradient shadow?

Approach: Here are the steps required to get the desired linear-gradient shadow: Make an HTML file, with the button, card, banner or the thing that you want to make shadow of. Make a pseudo class in stylesheet and make the position absolute of the pseudo class relative to the parent element.

How do you change gradient color in Swift storyboard?

There are many ways to create background gradients, below is just one simple approach: In a new Xcode iOS project, open Main. storyboard, from the Object Library drag a new View onto the View Controller. Set the View's top, bottom, left and right constraints to be zero and ensure 'Constrain to margins' is deselected.

How do you use gradient color in swift 5?

Creating a layer with gradient colors is quite simple. For the most basic gradient, it only requires the colors you want to use, and then you can optionally adjust color location. Once you've created your gradient, add it simply to your view layer by calling the addSublayer function.


2 Answers

I think you can't do more with standard CALayer shadow. Take a look at filters property of CALayer.

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

Create second CAGradientLayer and apply a GausianBlur filter for example.

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.

like image 91
Jacek Głazik Avatar answered Oct 21 '22 21:10

Jacek Głazik


Use my code for your requirement available in my gitRepo https://github.com/Krishnarjun-Banoth/BlurView/tree/master/Shadows

Step1: Just drag and drop BlurEffect.swift file in your project.

Step2: Then simply Use below View extension methods for each one your views individually.

  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.

like image 1
Krishnarjun Banoth Avatar answered Oct 21 '22 21:10

Krishnarjun Banoth