Skip to content

Instantly share code, notes, and snippets.

@oboje
Forked from ilyapuchka/UIVisualEffect.swift
Created January 21, 2017 12:26
Show Gist options
  • Save oboje/0485f3698cdb0f206e0ec1bc1acd859d to your computer and use it in GitHub Desktop.
Save oboje/0485f3698cdb0f206e0ec1bc1acd859d to your computer and use it in GitHub Desktop.
(Ab)using UIVisualEffectView effect settings
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
}
}
}
@iamtchelo
Copy link

Amazing extension. It works fine! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment