Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ScrollView内置自定义手势滑动卡,导致SwiftUI出现问题_Swift_Swiftui_Gesture - Fatal编程技术网

ScrollView内置自定义手势滑动卡,导致SwiftUI出现问题

ScrollView内置自定义手势滑动卡,导致SwiftUI出现问题,swift,swiftui,gesture,Swift,Swiftui,Gesture,我已经做了一个最小的可复制的例子,我将粘贴在下面。只需将其插入Xcode,您就可以看到交易内容了。基本上,我有一个自定义的幻灯片卡。当它的enum位置为.top时,我在Xcode模拟器中滑动ScrollView,它会导致卡的位置稍微移动。有没有办法锁定卡的位置?或者至少让滑动卡内的ScrollView在刷卡手势方面问题更少 内容视图: struct ContentView : View { var body: some View { ZStack(alignment: A

我已经做了一个最小的可复制的例子,我将粘贴在下面。只需将其插入Xcode,您就可以看到交易内容了。基本上,我有一个自定义的幻灯片卡。当它的enum位置为.top时,我在Xcode模拟器中滑动ScrollView,它会导致卡的位置稍微移动。有没有办法锁定卡的位置?或者至少让滑动卡内的ScrollView在刷卡手势方面问题更少

内容视图:

struct ContentView : View {
    var body: some View {
        ZStack(alignment: Alignment.top) {
            Text("test")
                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                .background(Color.red)
            SlideOverCard {
                VStack {
                    ScrollView {
                    VStack {
                        ForEach(1..<100) { _ in
                            Text("test")
                        }
                    }.frame(width: UIScreen.main.bounds.width)
                    }
                    Text("TESTER LINE OF TEXT")
                    Spacer()
                    
                    
                }.frame(width: UIScreen.main.bounds.width)
            }
        }
        .edgesIgnoringSafeArea(.vertical)
    }
}

好吧,我设法重现了你的意思

使用Xcode 12/iOS 14进行测试

更新:-这里找到了解决方案

ScrollView {
    VStack {
        ForEach(1..<100) { _ in
            Text("test")
        }
    }.frame(width: UIScreen.main.bounds.width)
}
.background(Color.white)             // << make opaque background
.highPriorityGesture(DragGesture())  // << block below DragGesture
滚动视图{
VStack{

弗雷奇(1..好的,ScrollView使用拖动手势,而您使用拖动手势,因此,很明显,它们之间存在冲突。但不清楚您期望或想要实现什么?@Asperi我只想在SlideUpCard内部有一个不会导致任何意外行为的ScrollView。我只想这样,如果我在ScrollView内部滑动,它不会导致pos发生变化当前,如果我在ScrollView中滑动,有时确实会改变SlideUpCard的位置。如果该手势在ScrollView中,有没有办法不让SlideUpCard注册该手势?@Asperi我制作了一个链接来显示该故障:但在你的代码中,ScrollView完全覆盖了SlideUpCard(只需添加边框即可查看),这就是我的问题的原因,因为如果你优先考虑滚动视图手势,你的卡片将完全停止响应,但如果两者都处于活动状态-存在冲突…因此代码的期望值是不明确的。@Asperi是的,我明白你的意思。这是我实际编码的一个简化示例。在我的实际版本中,有一个视图位于幻灯片太长了,读不下去了,大约200个像素,你可以使用句柄(和200个像素部分)来控制SLIDEUPARCKET的位置。TL;DR在我的非简化代码中,ScLoVIEW不占用整个卡,所以优先考虑滚动视图不会有问题。
struct Handle : View {
    private let handleThickness = CGFloat(5.0)
    var body: some View {
        RoundedRectangle(cornerRadius: handleThickness / 2.0)
            .frame(width: 40, height: handleThickness)
            .foregroundColor(Color.secondary)
            .padding(5)
    }
}
ScrollView {
    VStack {
        ForEach(1..<100) { _ in
            Text("test")
        }
    }.frame(width: UIScreen.main.bounds.width)
}
.background(Color.white)             // << make opaque background
.highPriorityGesture(DragGesture())  // << block below DragGesture
    return VStack {  // << make it instead of Group
        Handle()
            .gesture(drag)   // << here !!
        self.content()
    }
    .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    .background(Color.white)
    .cornerRadius(10.0)
    .shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.13), radius: 10.0)
    .offset(y: self.position.rawValue + self.dragState.translation.height)
    .animation(self.dragState.isDragging ? nil : .interpolatingSpring(stiffness: 300.0, damping: 30.0, initialVelocity: 10.0))