Created
February 13, 2016 06:07
-
-
Save afshin-hoseini/9c370268ffa4c43b0696 to your computer and use it in GitHub Desktop.
Custom blur view which you can determine blur radius for
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
// | |
// APCustomBlurView.swift | |
// Created by Collin Hundley on 1/15/16. | |
// | |
import UIKit | |
public class APCustomBlurView: UIVisualEffectView { | |
private let blurEffect: UIBlurEffect | |
public var blurRadius: CGFloat { | |
return blurEffect.valueForKeyPath("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 | |
} | |
blurEffect.setValue(radius, forKeyPath: "blurRadius") | |
self.effect = blurEffect | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment