简体   繁体   English

如何在 LazyVStack/ScrollView SwiftUI 中将图像置于前台?

[英]How to bring an Image to foreground in LazyVStack/ScrollView SwiftUI?

I have a LazyVStack of Post Views with images in those views (similar to Instagram).我有一个 LazyVStack of Post Views,这些视图中有图像(类似于 Instagram)。 When I'm zooming in on one of the images, I would like for that image to overlap over all other content.当我放大其中一张图片时,我希望该图片与所有其他内容重叠。 However, currently what's happening is it overlaps over posts above it, but goes under posts below it.然而,目前正在发生的事情是它重叠在它上面的帖子上,但在它下面的帖子下。 I'm pretty sure the reasoning behind this is the compiler assigns Zindexes to each element in a Stack and by default the later ones are given a higher priority Zindex value.我很确定这背后的原因是编译器将 Zindexes 分配给 Stack 中的每个元素,默认情况下,后面的元素会获得更高优先级的 Zindex 值。

I've tried playing around with the Zindexes but to no avail.我试过使用 Zindexes,但无济于事。 I've tried having a @Binding variable in the PostView that updates the @State variable in the Timeline view, but that didn't work either.我试过在 PostView 中使用 @Binding 变量来更新时间轴视图中的 @State 变量,但这也不起作用。 Firstly, here's a simplified version of my Timeline View首先,这是我的时间轴视图的简化版本

struct TimelineView: View {
    @State var isZooming: Bool = False
    @State var posts : [Post]
    var body: some View {
        LazyVStack {
            ForEach(posts) { post in
                PostView(post, $isZooming)
                    .zIndex(isZooming ? 1 : 0)
            }
        }
    }
}

And here's a simplified version of my Post View这是我的帖子视图的简化版本

struct PostView: View {
    var post : Post
    @Binding var isZooming: Bool
    var body: some View {
        VStack {
            post.caption
            post.image
                .gesture(MagnificationGesture().onChanged { amount in 
                    self.isZooming = true
                }   
        }
    }
}

Your zIndex approach is right, but if you apply it inside the ForEach it will be set for all posts, so they end up all being zIndex = 1.您的zIndex方法是正确的,但是如果您在ForEach中应用它,它将为所有帖子设置,因此它们最终都是 zIndex = 1。

If you move zIndex into the subview it works:如果将zIndex移动到子视图中,它会起作用:

struct TimelineView: View {
    
    @State var posts : [Post] = Post.dummyData
    
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(posts) { post in
                    PostView(post: post)
                }
                .padding()
            }
        }
    }
}

struct PostView: View {
    var post : Post
    
    @State var isZooming: Bool = false
    @State var zoom: CGFloat = 1
    
    var body: some View {
        VStack {
            Text(post.caption)
            Image(post.image)
                .resizable().scaledToFill()
                .frame(height: 250
                )
                .clipped()
                .scaleEffect(zoom)
            
                .gesture(MagnificationGesture()
                    .onChanged { value in
                        zoom = value
                        isZooming = true
                    }
                    .onEnded { _ in
                        zoom = 1
                        isZooming = false
                        
                    }
                )
        }
        .zIndex(isZooming ? 1 : 0) // here
    }
}

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

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