0

你好,滚动视图中有一个对象。进行拖动手势时,它不会超出滚动视图。我设置了zindex,但它不起作用。如何将文本移动到图片中出现的红色区域?

struct SwiftUIView2: View {
    @State private var dragAmount = CGSize.zero

    var body: some View {
        VStack{
            
            GeometryReader { geo in
                ScrollView() {
                    ScrollViewReader { reader in
                        Text("Hello, World!")
                            .background(Color.yellow)
                            .offset(dragAmount)
                            .gesture(
                                DragGesture(minimumDistance: 0, coordinateSpace: .global)
                                    .onChanged { value in
                                        self.dragAmount = CGSize(width: value.translation.width, height: value.translation.height)
                                    }
                                    .onEnded { value in
                                        self.dragAmount = .zero
                                    }
                            )
                    }
                }
            }
            .background(Color.blue)
            .frame(width: 222, height: 100)
            .zIndex(1)
            
            Color.red
                .frame(width: 222, height: 100)
                .zIndex(0)

            
        }
    }
}

截屏

4

0 回答 0