-
-
Save oboje/0485f3698cdb0f206e0ec1bc1acd859d to your computer and use it in GitHub Desktop.
(Ab)using UIVisualEffectView effect settings
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
extension UIVisualEffectView { | |
private var filterLayer: CALayer? { | |
return layer.sublayers?.first | |
} | |
private var blurFilter: NSObject? { | |
return filterLayer? | |
.filters?.flatMap({ $0 as? NSObject }) | |
.first(where: { $0.value(forKey: "name") as? String == "gaussianBlur" }) | |
} | |
private var saturationFilter: NSObject? { | |
return filterLayer? | |
.filters?.flatMap({ $0 as? NSObject }) | |
.first(where: { $0.value(forKey: "name") as? String == "colorSaturate" }) | |
} | |
var blurRadius: CGFloat { | |
get { | |
return blurFilter?.value(forKey: "inputRadius") as? CGFloat ?? 0 | |
} | |
set { | |
blurFilter?.setValue(newValue, forKey: "inputRadius") | |
if newValue == 0 { scale = 1 } | |
} | |
} | |
var saturationAmount: CGFloat { | |
get { | |
return saturationFilter?.value(forKey: "inputAmount") as? CGFloat ?? 1 | |
} | |
set { | |
saturationFilter?.setValue(newValue, forKey: "inputAmount") | |
} | |
} | |
var scale: CGFloat? { | |
get { | |
return filterLayer?.value(forKey: "scale") as? CGFloat | |
} | |
set { | |
filterLayer?.setValue(newValue, forKey: "scale") | |
} | |
} | |
var opacity: Float? { | |
get { | |
return layer.sublayers?.last?.opacity | |
} | |
set { | |
layer.sublayers?.last?.opacity = newValue ?? 0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing extension. It works fine! =)