How can I can add a gradient view that has blur effects in Swift? I can add a gradient layer (CAGradientLayer) quite easily to a view. I can also add a blur view (UIVisualEffectView) separately.
How can I combine both to create a blur view that also has a gradient element where by full blur fading to no blur?
An example of a similiar effect:
To achieve a blur view with gradient blur radius, you can do the following (per Apple document):
Here,
inputPoint0
is the starting point of gradient ramp,inputPoint1
is the ending point. Note that forCIVector
y increases from bottom to top. The input color you set doesn't matter, only its alpha is used to determine blur radius.For your second question of dynamically applying CIFilter to an image underneath it, no, it's not possible. There is an Apple document on
backgroundFilters
that might make you think it is doable, until you see at the bottom ...What you should do is that, whenever you reset the image, apply the above operation to that image and set it to the image view.
If you know CoreImage, you can easily chain two filters together, one from the CICategoryBlur and one from the category CICategoryGradient.
Here's an example of usage. TO chain, just take the output of the first filter as the input of the next:
The link above is to the Apple documentation.