Created
February 28, 2020 09:49
-
-
Save Sjahriyar/63861f42baf2636abfec611f6b97f432 to your computer and use it in GitHub Desktop.
UIVisualBlurEffect with adjustable radius
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
// | |
// CSBlurEffect.swift | |
// SHS | |
// | |
// Created by Shahriyar Soheili on 28/02/2020. | |
// Copyright © 2020 Shahriyar Soheili. All rights reserved. | |
// | |
import UIKit | |
public class CSBlurEffect: UIVisualEffectView | |
{ | |
private let blurEffect: UIBlurEffect | |
public var blurRadius: CGFloat { | |
return self.blurEffect.value(forKeyPath: "blurRadius") as! CGFloat | |
} | |
public convenience init() { | |
self.init(withRadius: 0) | |
} | |
public init(withRadius radius: CGFloat) { | |
let customBlurClass: AnyObject.Type = NSClassFromString("_UICustomBlurEffect")! | |
let customBlurObject: NSObject.Type = customBlurClass as! NSObject.Type | |
self.blurEffect = customBlurObject.init() as! UIBlurEffect | |
self.blurEffect.setValue(1.0, forKeyPath: "scale") | |
self.blurEffect.setValue(radius, forKeyPath: "blurRadius") | |
super.init(effect: radius == 0 ? nil : self.blurEffect) | |
} | |
required public init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
public func setBlurRadius(radius: CGFloat) { | |
guard radius != blurRadius else { | |
return | |
} | |
self.blurEffect.setValue(radius, forKeyPath: "blurRadius") | |
self.effect = self.blurEffect | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment