在SwiftUI中使用DragGesture()计算不正确的高度

问题描述 投票:0回答:1

[我正在使用SwiftUI做一个基于Map的应用程序,我需要通过上下拖动来更改模式卡的高度(这是该地图信息所在的视图),就像在Apple Maps中一样。

[这是Card的屏幕截图,身高我试图与之互动https://i.stack.imgur.com/mZX2m.png

我用ZStack实现了RoundedRectangle,并添加了DragGesture()修饰符。

            ZStack(alignment: .top){
                RoundedRectangle(cornerRadius: 16.0)
                    .frame(height:currentHeight)
                Text("Card")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
                    .multilineTextAlignment(.center)
                    .padding(.top)
            }.gesture(DragGesture()
                .onChanged { value in
                    if (self.currentHeight + value.predictedEndLocation.y - value.startLocation.y > UIScreen.main.bounds.height) {
                        self.currentHeight = UIScreen.main.bounds.height
                    } else {
                        self.currentHeight += value.predictedEndLocation.y - value.startLocation.y
                    }
                }
                .onEnded { value in
                    if (self.currentHeight + value.predictedEndLocation.y - value.startLocation.y > UIScreen.main.bounds.height) {
                        self.currentHeight = UIScreen.main.bounds.height
                    } else {
                        self.currentHeight += value.predictedEndLocation.y - value.startLocation.y
                    }
                })

[第一个问题是向上拖动,卡超过了屏幕高度,我检查了UIScreen边界高度以防止高度大于屏幕,但是计算存在更多问题,卡的高度非常快速地变得模棱两可,并且没有要向下拖动。

我从未与手势识别器合作。你能告诉我正确的计算方法吗?

谢谢!

ios swiftui drag
1个回答
0
投票

这是一种方法(下面的代码中一些有用的注释)

“

struct TestResizingCard: View {

    static let kMinHeight: CGFloat = 100.0
    @State var currentHeight: CGFloat = kMinHeight // << any initial

    var body: some View {
        GeometryReader { g in // << for top container height limit
            ZStack(alignment: .bottom) {
                Rectangle().fill(Color.yellow) // << just for demo

                self.card()
                .gesture(DragGesture()
                    .onChanged { value in
                        // as card is at bottom the offset is reversed
                        let newHeight = self.currentHeight - (value.location.y - value.startLocation.y)
                        if newHeight > Self.kMinHeight && newHeight < g.size.height {
                            self.currentHeight = newHeight
                        }
            })
            }
        }
    }

    func card() -> some View {
        ZStack(alignment: .top){
            RoundedRectangle(cornerRadius: 16.0)
                .frame(height:currentHeight)
            Text("Card")
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.center)
                .padding(.top)
        }
    }
}

struct TestResizingCard_Previews: PreviewProvider {
    static var previews: some View {
        TestResizingCard()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.