Created
April 18, 2020 21:50
-
-
Save siempay/bc96d9c86408aa7924c080bff5b28a8f to your computer and use it in GitHub Desktop.
Add blur effect on a UIView with blurRadius
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+BlurRadius.swift | |
// PrecisionSystem | |
// | |
// Created by Siempay on 3/27/20. | |
// Copyright © 2020 Brahim ELMSSILHA. All rights reserved. | |
// | |
import Foundation | |
extension UIView { | |
/// Add UIBlurEffect on top of this view | |
/// | |
/// This helper add a blur effect with possibility | |
/// of reducing the amount (radius) of the blur effect. | |
/// | |
/// By using default parameters: you will get the half amount | |
/// of blur effect you normaly get. | |
/// | |
/// - Parameters: | |
/// - style: blur style: .dark, .light ... | |
/// - radius: from 0 mean nothing to 1 means normal blur | |
func addBlurEffect(style: UIBlurEffect.Style = .dark, radius: Double = 0.5) { | |
// create new blur effect and added as subView | |
let blurEffect = UIBlurEffect(style: style) | |
let blurEffectView = UIVisualEffectView() | |
addSubview(blurEffectView) | |
// always fill the view | |
blurEffectView.frame = self.bounds | |
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
// make the blur being with animation | |
// then pausing the animation | |
UIView.animate(withDuration: 1) { | |
blurEffectView.effect = blurEffect | |
} | |
blurEffectView.pauseAnimation(delay: radius) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment