微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

复制用于自定义控件的 SwiftUI GraphicalDatePickerStyle 的背景模糊

如何解决复制用于自定义控件的 SwiftUI GraphicalDatePickerStyle 的背景模糊

我正在尝试开发一个自定义控件,用于在行为方式与 SwiftUI 的 DatePicker 类似的表单中使用(在表单中,它认为 GraphicalDatePickerStyle)。

Apple 的控件以模态显示日历和时间选择器,背景模糊。我可以使用 .fullScreenCover 修饰符管理模式演示。通过向显示控件的背景添加 UIVisualEffectView 也可以实现背景模糊。然而,即使使用系统认提供的最轻量级的选项,以这种方式应用的模糊效果也过于沉重。我也对通过 dispatchQueue.main.async 修改超级视图背景的依赖感到不满 - 它有效但让我感到不舒服。

有什么方法可以更接近 Apple 的实施吗?我尝试添加活力效果层并没有让我在任何地方,而且根据文档,您不应该触摸 UIEffectViews 的 alpha...

以下视觉效果(所需和当前迭代)的代码和比较:

import SwiftUI

struct ContentView: View {
    @State private var isPresented = false
    @State private var interval: TimeInterval = 0
    
    var body: some View {
        Form {
            Text("Row")
            Text("Row")
            Text("Row")
            Text("Row")
            
            DatePicker("Date",selection: .constant(Date()))
            
            Button("Present custom control") {
                self.isPresented.toggle()
            }
            .fullScreenCover(isPresented: $isPresented) {
                ZStack {
                    Color.white.opacity(0.1)
                        .edgesIgnoringSafeArea(.all)
                    // below is a placeholder for the custom control
                    Button("dismiss") { isPresented.toggle() }
                        .frame(width: 200,height: 200)
                        .background(Color.white)
                        .cornerRadius(16)
                }
                .background(BackgroundBlurView())
            }
            
            Text("Other stuff here")
                .background(Color.red)
        }
    }
}

struct BackgroundBlurView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterial))
        // this works but it looks a bit hacky... code smell?
        dispatchQueue.main.async {
            view.superview?.superview?.backgroundColor = .clear
        }
        return view
    }
    
    func updateUIView(_ uiView: UIView,context: Context) {
        // nothing needed here
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

表单(点击控件前)

Form (before control tapped)

DatePicker 应用的背景模糊

Background blur applied by DatePicker

我的尝试 - 不是远程关闭

Current attempt - significant blur and loss of vibrancy

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。