SwiftUI:如何实现SwipGesture?

时间:2019-11-16 13:38:46

标签: swift swiftui

我正在学习SwiftUI,为此,我正在开发一个小应用程序,该应用程序结合了许多特定于SwiftUI的新功能。

但是,我想创建一个UISwipeGestureRecognizer以便能够在UIViewRepresentable中进行导航。

基本上,用户到达主页,可以向左或向右滑动,将他带到相关视图。

此外,我只想注意到我已经做了很多研究,但是在SwiftUI中我没有看到关于SwipeGesture的任何信息(AppleDocumentation非常简短,并且像我一样,没有显示Noobies的范例!)< / p>

这是我目前的代码:


struct SheetMenu: View {

    @State var currentPage = 0


    var body: some View {
        GeometryReader { geo in
            ZStack {
                if self.currentPage == 0 {
                    Text("Page 1")
                }
                else if self.currentPage == 1 {
                    Text("Page 2")
                }

                else if self.currentPage == 2 {
                    Text("Page 3")
                }

                else if self.currentPage == 3 {
                    Text("Page 4")
                }

                else if self.currentPage == 4 {
                    Text("Page 5")
                }

                else if self.currentPage == 5 {
                    Text("Page 6")
                }

                else if self.currentPage == 6 {
                    Text("Page 7")
                }



            }
            .backGroundColor(colorBackGround)
            PageControl(current: self.currentPage)
                .position(x: geo.size.width/2, y: geo.size.height)



        }

    }
}

struct PageControl : UIViewRepresentable {

    var current = 0

    func makeUIView(context: UIViewRepresentableContext<PageControl>) -> UIPageControl {
        let page = UIPageControl()
        page.numberOfPages = 7
        page.currentPageIndicatorTintColor = .black
        page.pageIndicatorTintColor = .gray
        return page
    }

    func updateUIView(_ uiView: UIPageControl, context: UIViewRepresentableContext<PageControl>) {
        uiView.currentPage = current
    }
}


感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

手势是视图上的修饰符。您可以通过拖动来伪造滑动(您可能要为该手势添加超时)。请注意,我必须展开您的“文本”框架,否则该手势仅在您位于“文本”顶部时才能识别:

struct ContentView: View {

  @State var currentPage = 0

  var body: some View {
    GeometryReader { geo in
      PageControl(current: self.currentPage)
        .position(x: geo.size.width/2, y: geo.size.height)
      Text("Page: \(self.currentPage)")
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .gesture(
        DragGesture()
          .onEnded {
            if $0.translation.width < -100 {
              self.currentPage = max(0, self.currentPage - 1)
            } else if $0.translation.width > 100 {
              self.currentPage = min(6, self.currentPage + 1)
            }
        }
      )
    }
  }
}

struct PageControl : UIViewRepresentable {

  var current = 0

  func makeUIView(context: UIViewRepresentableContext<PageControl>) -> UIPageControl {
    let page = UIPageControl()
    page.numberOfPages = 7
    page.currentPageIndicatorTintColor = .black
    page.pageIndicatorTintColor = .gray
    return page
  }

  func updateUIView(_ uiView: UIPageControl, context: UIViewRepresentableContext<PageControl>) {
    uiView.currentPage = current
  }
}