Skip to content

Instantly share code, notes, and snippets.

@siempay
Created April 18, 2020 21:50
Show Gist options
  • Save siempay/bc96d9c86408aa7924c080bff5b28a8f to your computer and use it in GitHub Desktop.
Save siempay/bc96d9c86408aa7924c080bff5b28a8f to your computer and use it in GitHub Desktop.
Add blur effect on a UIView with blurRadius
//
// 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