swift 无法使滑动底板不低于最小高度

brccelvz  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(107)

我有一个简单的底表,我想不低于一定的高度(200分)下降。
然而,每当我的底部工作表滚动到底部,它从屏幕上掉下来,它来无法使用。我想动画是没有捕捉点平滑。我目前的实现似乎做了我想做的一切,但我似乎不能让它停止超出底部的界限。
是否有一个高度调整或逻辑表达式,我应该把在偏移视图修改器,以获得这种行为?
谢谢你!

public struct FloatingPanel: View {
    // New properties
    @State var offset: CGFloat = 200
    @State var isInitialOffsetSet: Bool = false
    
    public init()
    
    public var body: some View {
        GeometryReader { proxy in
            ZStack {
                Color.white
                    .clipShape(CustomCorner(corners: [.topLeft, .topRight], radius: 20))
                
                VStack {
                    Capsule()
                        .fill(Color.gray)
                        .frame(width: 60, height: 4)
                        .padding(.top, 5)
                    
                    NavigationView {
                        VStack {
                            Text("this is navigation content")
                        }
                        .background(Color.white)
                    }
                    .navigationViewStyle(.stack)
                    
                    Spacer()
                }
            }
        }
        .offset(y: offset)
        .gesture(
            DragGesture()
                .onChanged { value in
                    withAnimation {
                        let startLocation = value.startLocation
                        offset = startLocation.y + value.translation.height
                    }
                }
        )
        .onAppear {
            if !isInitialOffsetSet {
                offset = UIScreen.main.bounds.height - 250
                isInitialOffsetSet = true
            }
        }
        .ignoresSafeArea(.all, edges: .all)
    }
}

struct CustomCorner: Shape {
    var corners: UIRectCorner
    var radius: CGFloat

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}
kyvafyod

kyvafyod1#

您可以使用min()为偏移量添加下限。新的偏移线为:.offset(y: min(UIScreen.main.bounds.height - 200, offset))
使用minHeight变量实现:

public struct FloatingPanel: View {
    // New properties
    @State var offset: CGFloat = 200
    @State var isInitialOffsetSet: Bool = false
    
    let minHeight: CGFloat = 200
    
    public init() {}
    
    public var body: some View {
        GeometryReader { proxy in
            ZStack {
                Color.white
                    .clipShape(CustomCorner(corners: [.topLeft, .topRight], radius: 20))
                
                VStack {
                    Capsule()
                        .fill(Color.gray)
                        .frame(width: 60, height: 4)
                        .padding(.top, 5)
                    
                    NavigationView {
                        VStack {
                            Text("this is navigation content")
                        }
                        .background(Color.white)
                    }
                    .navigationViewStyle(.stack)
                    
                    Spacer()
                }
            }
        }
        .offset(y: min(UIScreen.main.bounds.height - minHeight, offset))
        .gesture(
            DragGesture()
                .onChanged { value in
                    withAnimation {
                        let startLocation = value.startLocation
                        offset = startLocation.y + value.translation.height
                    }
                }
        )
        .onAppear {
            if !isInitialOffsetSet {
                offset = UIScreen.main.bounds.height - 250
                isInitialOffsetSet = true
            }
        }
        .ignoresSafeArea(.all, edges: .all)
    }
}

struct CustomCorner: Shape {
    var corners: UIRectCorner
    var radius: CGFloat

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}

(Also建议您研究一下Presentation Detents-- Apple原生解决方案)

相关问题