Last active
January 21, 2020 21:57
-
-
Save ilyapuchka/d1c20610edc3e97a7dc97365aa9fe1cc 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