-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
76 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
![ci](https://github.com/swift-extensions/swiftui-visual-effects/workflows/ci/badge.svg) | ||
|
||
## SwiftUI Visual Effects | ||
|
||
### VisualEffectBlur | ||
```swift | ||
.visualEffect(material: .sidebar, blendingMode: .behindWindow) | ||
.background( | ||
VisualEffectBlur() | ||
) | ||
``` | ||
|
||
## Code Contributions | ||
Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue. | ||
|
||
## Coffee Contributions | ||
If you find this project useful please consider becoming my GitHub sponsor. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#if os(iOS) | ||
import SwiftUI | ||
|
||
public struct VisualEffectBlur<Content: View>: View { | ||
var blurStyle: UIBlurEffect.Style | ||
var vibrancyStyle: UIVibrancyEffectStyle? | ||
var content: Content | ||
|
||
public init(blurStyle: UIBlurEffect.Style = .systemMaterial, vibrancyStyle: UIVibrancyEffectStyle? = nil, @ViewBuilder content: () -> Content) { | ||
self.blurStyle = blurStyle | ||
self.vibrancyStyle = vibrancyStyle | ||
self.content = content() | ||
} | ||
|
||
public var body: some View { | ||
Representable(blurStyle: blurStyle, vibrancyStyle: vibrancyStyle, content: ZStack { content }) | ||
.accessibility(hidden: Content.self == EmptyView.self) | ||
} | ||
} | ||
|
||
extension VisualEffectBlur { | ||
struct Representable<Content: View>: UIViewRepresentable { | ||
var blurStyle: UIBlurEffect.Style | ||
var vibrancyStyle: UIVibrancyEffectStyle? | ||
var content: Content | ||
|
||
func makeUIView(context: Context) -> UIVisualEffectView { | ||
context.coordinator.blurView | ||
} | ||
|
||
func updateUIView(_ view: UIVisualEffectView, context: Context) { | ||
context.coordinator.update(content: content, blurStyle: blurStyle, vibrancyStyle: vibrancyStyle) | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(content: content) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Coordinator | ||
|
||
extension VisualEffectBlur.Representable { | ||
class Coordinator { | ||
let blurView = UIVisualEffectView() | ||
let vibrancyView = UIVisualEffectView() | ||
let hostingController: UIHostingController<Content> | ||
|
||
init(content: Content) { | ||
hostingController = UIHostingController(rootView: content) | ||
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] | ||
hostingController.view.backgroundColor = nil | ||
blurView.contentView.addSubview(vibrancyView) | ||
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | ||
vibrancyView.contentView.addSubview(hostingController.view) | ||
vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | ||
} | ||
|
||
func update(content: Content, blurStyle: UIBlurEffect.Style, vibrancyStyle: UIVibrancyEffectStyle?) { | ||
hostingController.rootView = content | ||
let blurEffect = UIBlurEffect(style: blurStyle) | ||
blurView.effect = blurEffect | ||
if let vibrancyStyle = vibrancyStyle { | ||
vibrancyView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyStyle) | ||
} else { | ||
vibrancyView.effect = nil | ||
} | ||
hostingController.view.setNeedsDisplay() | ||
} | ||
} | ||
} | ||
|
||
public extension VisualEffectBlur where Content == EmptyView { | ||
init(blurStyle: UIBlurEffect.Style = .systemMaterial, vibrancyStyle: UIVibrancyEffectStyle? = nil) { | ||
self.init(blurStyle: blurStyle, vibrancyStyle: vibrancyStyle) { | ||
EmptyView() | ||
} | ||
} | ||
} | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#if os(macOS) | ||
import SwiftUI | ||
|
||
public struct VisualEffectBlur: View { | ||
private var material: NSVisualEffectView.Material | ||
private let blendingMode: NSVisualEffectView.BlendingMode | ||
private let isEmphasized: Bool | ||
|
||
public init(material: NSVisualEffectView.Material = .headerView, blendingMode: NSVisualEffectView.BlendingMode = .withinWindow, isEmphasized: Bool = false) { | ||
self.material = material | ||
self.blendingMode = blendingMode | ||
self.isEmphasized = isEmphasized | ||
} | ||
|
||
public var body: some View { | ||
Representable(material: material, blendingMode: blendingMode, isEmphasized: isEmphasized) | ||
.accessibility(hidden: true) | ||
} | ||
} | ||
|
||
extension VisualEffectBlur { | ||
struct Representable: NSViewRepresentable { | ||
var material: NSVisualEffectView.Material | ||
var blendingMode: NSVisualEffectView.BlendingMode | ||
var isEmphasized: Bool | ||
|
||
func makeNSView(context: Context) -> NSVisualEffectView { | ||
context.coordinator.visualEffectView | ||
} | ||
|
||
func updateNSView(_ view: NSVisualEffectView, context: Context) { | ||
context.coordinator.update(material: material, blendingMode: blendingMode, isEmphasized: isEmphasized) | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
} | ||
|
||
class Coordinator { | ||
let visualEffectView = NSVisualEffectView() | ||
|
||
func update(material: NSVisualEffectView.Material, blendingMode: NSVisualEffectView.BlendingMode, isEmphasized: Bool) { | ||
visualEffectView.material = material | ||
visualEffectView.blendingMode = blendingMode | ||
visualEffectView.isEmphasized = isEmphasized | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.