正在尝试在SWIFT Ui中创建滑动确认按钮

jchrr9hc  于 2022-09-19  发布在  Swift
关注(0)|答案(1)|浏览(159)

因此,我在网络和许多应用程序上看到了许多人通过滑动来购买按钮。我认为在我的应用程序上实现它会很好。但由于某些原因,ZStack不能正常工作,背景在滑动箭头的顶部。我不能正确地将它放在另一个视图中。此外,当可滑动的箭头完全位于末端时,如何执行动作?

import SwiftUI

struct ContentView: View {
    @State var viewState = CGSize(width: 0, height: 50)

    var body: some View {

        ZStack{

/// This part is the background of the swipe to buy 

            RoundedRectangle(cornerRadius: 10)
                .offset(x: viewState.width)
                .frame(width: 800,height: 100)

            HStack{

// This part is the actual swiping arrow 

                ZStack {
                    RoundedRectangle(cornerRadius: 30)
                        .fill(Color.blue)
                        .frame(width: 100, height: 100)
                        .offset(x: viewState.width)
                        .gesture(
                            DragGesture().onChanged { value in
                                viewState = value.translation
                            }
                            .onEnded { value in
                                withAnimation(.spring()) {
                                    viewState = .zero
                                }
                            }
                    )

                    Image(systemName: "chevron.right")
                        .offset(x: viewState.width)
                        .gesture(
                            DragGesture().onChanged { value in
                                viewState = value.translation
                            }
                            .onEnded { value in
                                withAnimation(.spring()) {
                                    viewState = .zero
                                }
                            }
                    )

                }

                Spacer()
            }

        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
'''
5m1hhzi4

5m1hhzi41#

您的ZStack运行良好,问题是您给了top RoundedRectangleFIXWidth=800,并且您使用Spacer()将HStack推送到领先端。你必须去掉这个宽度,才能工作得这么好,就像这样:

RoundedRectangle(cornerRadius: 10)
        .offset(x: viewState.width)
        .frame(width: 800, height: 100)

但是如果您想要固定宽度,您可以使用Backging()而不是ZStack,如下所示:

HStack{

       ZStack {

            RoundedRectangle(cornerRadius: 30)
                 .fill(Color.blue)
                 .frame(width: 100, height: 100)
                 .offset(x: viewState.width)
                 .gesture(
                     DragGesture().onChanged { value in
                          viewState = value.translation
                     }
                          .onEnded { value in
                              withAnimation(.spring()) {
                                 viewState = .zero
                              }
                          }
                   )

            Image(systemName: "chevron.right")
                .offset(x: viewState.width)
                .gesture(
                    DragGesture().onChanged { value in
                         viewState = value.translation
                    }
                         .onEnded { value in
                              withAnimation(.spring()) {
                                   viewState = .zero
                              }
                         }
                 )

              }

             Spacer()
          }.background(

            RoundedRectangle(cornerRadius: 10)
                .offset(x: viewState.width)
                .frame(width: 800,height: 100)
        )

相关问题