ScrollView + DragGesture onChanged 冲突

问题描述

我一直在尝试创建一个底部工作表视图,其中包含几个视图,例如滚动视图。然而,在这部分源代码工作正常,我发现当手指首先与滚动视图交互时,onChanged 侦听器被触发,但拖动手势无法捕捉 onEnded。这就是 ui 无法处理拖动结束操作的原因。我认为这是一个已知问题,但我既找不到任何可接受的解决方案,也没有说服力的解释。请您给我一些反馈,也许我需要改变我的观点并重新设计源代码

谢谢。

struct UserInfoRegistrationView: View {

@Observedobject private var viewmodel: UserInfoRegistrationviewmodel

@State private var offSet: CGFloat = 0

init(viewmodel: UserInfoRegistrationviewmodel) {
    self.viewmodel = viewmodel
}

var body: some View {
   
    vstack {
        
        Spacer()
        
        vstack(alignment: .center,spacing: 0) {
            
                Rectangle()
                    .fill(Color.clear)
                    .frame(height: 40,alignment: .center)
            
            ScrollView(.vertical) {
                
                Rectangle()
                    .fill(Color.clear)
                    .frame(height: 40,alignment: .center)
                
                vstack(alignment: .leading,spacing: 30) {
                    
                    vstack(alignment: .leading,spacing: 15) {
                        
                        Text("Text 1")
                        Text("Text 2")
                        Text("Text 3")
                        Text("Text 4")
                }
                .padding(.horizontal,30)
                
            }
            .frame(height: UIScreen.main.bounds.height - 300)
            
        }
        .frame(maxWidth: .infinity)
        .background(
            Color.white
                .ignoresSafeArea()
        )
        .offset(y: offSet)
        .offset(y: viewmodel.showSheet ? 0 : (UIScreen.main.bounds.height - 200))
        .gesture(DragGesture()
                    .onEnded(self.onEnded(value:))
                    .onChanged(self.onChanged(value:)))
        
    }
    .background(
    
        Color.black.opacity(viewmodel.showSheet ? 0.4 : 0)
            .ignoresSafeArea()
            .onTapGesture(perform: onTapGesture)
        
    )
    
}

func onTapGesture() {
    withAnimation {
        viewmodel.showSheet.toggle()
    }
}

func onChanged(value: DragGesture.Value) {
    
    if value.translation.height > 0 {
        offSet = value.translation.height
        debugPresentationLayer("offset : \(offSet)")
    }
}

func onEnded(value: DragGesture.Value) {
    
    withAnimation(Animation.easeIn(duration: 0.4)) {
        
        let height = UIScreen.main.bounds.height / 3
        
        if value.translation.height > height {
            viewmodel.showSheet.toggle()
        }
        offSet = 0.0
        
    }
    
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)