毛玻璃效果自定义值
class AlphaBlurView: UIVisualEffectView {
private let blurEffect: UIBlurEffect
public var blurRadius: CGFloat {
return blurEffect.value(forKeyPath: "blurRadius") as! CGFloat
}
public convenience init() {
self.init(withRadius: 0)
}
public init(withRadius radius: CGFloat,tintColor: UIColor = #colorLiteral(red: 0.07843137255, green: 0.07843137255, blue: 0.07843137255, alpha: 0.9)) {
let customBlurClass: AnyObject.Type = NSClassFromString("_UICustomBlurEffect")!
let customBlurObject: NSObject.Type = customBlurClass as! NSObject.Type
self.blurEffect = customBlurObject.init() as! UIBlurEffect
self.blurEffect.setValue(1, forKeyPath: "scale")
self.blurEffect.setValue(radius, forKeyPath: "blurRadius")
self.blurEffect.setValue(tintColor, forKeyPath: "colorTint")
self.blurEffect.setValue(1, forKeyPath: "colorTintAlpha")
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
}
}
网友评论