简体   繁体   English

在 Swiftui 中模糊背景时返回视图

[英]Returning a view when blurred background in Swiftui

I want to view a page when the button is pressed.我想在按下按钮时查看页面。 I want the button to blur the background and also overlay text instead of just the blurred background.我希望按钮模糊背景并覆盖文本,而不仅仅是模糊的背景。 I am having trouble showing the blurred background while showing the text.我在显示文本时无法显示模糊的背景。 Please look at my code below...请在下面查看我的代码...

Where the problem is happening->>哪里出了问题->>

Button {
   withAnimation {
       show.toggle()
   }
} label: {
   VStack {
       ZStack {
          Rectangle()
             .fill(Color.white.opacity(0.5))
             .frame(maxWidth: 170, maxHeight: 60)
             .cornerRadius(15)
             .padding([.leading, .trailing])
          Image(systemName: "pause")
              .font(.title)
              .foregroundColor(.black)
                                }
                            }
                        }
                    }
                    .padding(.trailing, 6)
                    .padding(.bottom)
                    Spacer(minLength: 65)
                }
            }
        }.blur(radius: show ? 100 : 0)

预览图像

You can use this approach to achieve what you're looking for您可以使用这种方法来实现您想要的

import SwiftUI

struct BlurBackground: View {
    // MARK: - PROPERTIES
    
    @State private var showNextView: Bool = false
    
    // MARK: - BODY
    
    var body: some View {
        ZStack{
            Color.orange
                .edgesIgnoringSafeArea(.all)
                .blur(radius: showNextView ? 8.0 : 0.0) //Blur
            
            VStack(spacing: 40){
                Text("First View")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                
                Image(systemName: "apps.iphone")
                    .resizable()
                    .scaledToFit()
                    .frame(height: 200)
                    .foregroundColor(.white)
                
                Button("Show Next View") {
                    showNextView = true
                }
                .padding(.horizontal, 30)
                .padding(.vertical, 15)
                .background(.white)
            }//: VSTACK
            .blur(radius: showNextView ? 8.0 : 0.0) //Blur
            
            if showNextView{
                VStack {
                    Text("Second View")
                        .font(.largeTitle)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                    
                    Image(systemName: "laptopcomputer")
                        .resizable()
                        .scaledToFit()
                        .frame(height: 200)
                        .foregroundColor(.white)
                }//: VSTACK
            }
        }//: ZSTACK
    }
}

// MARK: - PREVIEW

struct BlurBackground_Previews: PreviewProvider {
    static var previews: some View {
        BlurBackground()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM