如何在以摄影机为父SwiftUI的scrollview顶部应用不透明度

如何在以摄影机为父SwiftUI的scrollview顶部应用不透明度,swiftui,Swiftui,我在相机视图中有一个滚动视图,我需要在滚动视图的顶部应用不透明度 实际/预期行为 我已经试过了,但没有成功 var body: some View { ZStack(alignment: .top) { VStack { } .frame( minWidth: 0, maxWidth: .infinity, minHeight: 0,

我在相机视图中有一个滚动视图,我需要在滚动视图的顶部应用不透明度

实际/预期行为

我已经试过了,但没有成功

var body: some View {
    ZStack(alignment: .top) {
        VStack {
            
        }
        .frame(
            minWidth: 0,
            maxWidth: .infinity,
            minHeight: 0,
            maxHeight: 10
        )
        .opacity(0.5)
        
        ScrollView(.vertical, showsIndicators: false) {
            ScrollViewReader { scrollView in
                VStack(alignment: .leading, spacing: 16) {
                    ForEach(messages) { message in
                        LazyVStack(alignment: .leading, spacing: 4) {
                            Text("\(message.sender)")
                                .kerning(0.02)
                                .fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
                                .font(.system(size: 13))
                                .lineSpacing(21)
                                .foregroundColor(Color.white)
                            Text("\(message.text)")
                                .kerning(0.02)
                                .fontWeight(.medium)
                                .font(.system(size: 13))
                                .lineSpacing(21)
                                .foregroundColor(Color.white)
                        }
                    }
                }
                .onChange(of: messages) { _ in
                    scrollView.scrollTo(messages.count + 1, anchor: .bottom)
                }
                .onAppear {
                    scrollView.scrollTo(messages.count, anchor: .bottom)
                }
            }
        }
    }
}

如何在此滚动视图顶部应用不透明度?

我使用以下代码解决了此问题

func getNameOpacity(_ distance: CGFloat) -> Double {
    return distance < 3 ? 1 : distance < 5 ? 0.7 : distance < 7 ? 0.5 : distance < 9 ? 0.3 : 0
}

func getTextOpacity(_ distance: CGFloat) -> Double {
    return distance < 23 ? 1 : distance < 25 ? 0.7 : distance < 27 ? 0.5 : distance < 29 ? 0.3 : 0
}

var body: some View {
    GeometryReader { outsideProxy in
        ScrollView(.vertical, showsIndicators: false) {
            ScrollViewReader { scrollView in
                VStack(alignment: .leading, spacing: 16) {
                    ForEach(messages) { message in
                        GeometryReader { insideProxy in
                            let distanceFromTop = outsideProxy.frame(in: .global).minY - insideProxy.frame(in: .global).minY + 6
                            let nameOpacity = getNameOpacity(distanceFromTop)
                            let textOpacity = getTextOpacity(distanceFromTop)
                            
                            VStack(alignment: .leading, spacing: 4) {
                                Text("\(message.sender)")
                                    .kerning(0.02)
                                    .fontWeight(.bold)
                                    .font(.system(size: 13))
                                    .lineSpacing(21)
                                    .foregroundColor(Color.white)
                                    .opacity(nameOpacity)
                                Text("\(message.text)")
                                    .kerning(0.02)
                                    .fontWeight(.medium)
                                    .font(.system(size: 13))
                                    .lineSpacing(21)
                                    .foregroundColor(Color.white)
                                    .opacity(textOpacity)
                            }
                        }
                    }
                    .frame(width: 100, height: 35, alignment: .center)
                }
                .onChange(of: messages) { _ in
                    scrollView.scrollTo(messages.count + 1, anchor: .bottom)
                }
                .onAppear {
                    scrollView.scrollTo(messages.count, anchor: .bottom)
                }
            }
        }
    }
}
func getnamepacity(u距离:CGFloat)->Double{
返回距离<3?1:距离<5?0.7:距离<7?0.5:距离<9?0.3:0
}
func gettexcapacity(u距离:CGFloat)->Double{
返回距离<23?1:距离<25?0.7:距离<27?0.5:距离<29?0.3:0
}
var body:一些观点{
GeometryReader{outsideProxy in
滚动视图(.vertical,showsIndicators:false){
ScrollViewReader{scrollView in
VStack(对齐:。前导,间距:16){
ForEach(messages){messagein
GeometryReader{insideProxy in
让distanceFromTop=outsideProxy.frame(in:.global).minY-insideProxy.frame(in:.global).minY+6
让nameOpacity=getNameOpacity(距离顶部)
让textOpacity=getExtracapity(距离顶部)
VStack(对齐:。前导,间距:4){
文本(“\(message.sender)”)
.kerning(0.02)
.fontWeight(.粗体)
.font(.system(大小:13))
.行距(21)
.foregroundColor(颜色.白色)
.不透明度(名称不透明度)
文本(“\(message.Text)”)
.kerning(0.02)
.fontWeight(.medium)
.font(.system(大小:13))
.行距(21)
.foregroundColor(颜色.白色)
.不透明度(文本不透明度)
}
}
}
.框架(宽:100,高:35,对齐:。中心)
}
.onChange(of:messages){in
scrollView.scrollTo(messages.count+1,锚点:。底部)
}
奥纳佩尔先生{
scrollView.scrollTo(messages.count,锚点:.bottom)
}
}
}
}
}

这是否回答了您的问题?我也尝试过,但没有成功。这是您的特例,因为您没有定义的背景颜色(您有视频)。所以你基本上要用面具。我做了一个例子,但是这将停用滚动行为,这在SwiftUIdavidev中仍然是一个bug,所以没有办法这么做?