简体   繁体   English

SwiftUI:如何实现 SwipGesture?

[英]SwiftUI : How to implement SwipGesture?

I am learning SwiftUI and for that, I am developing a small application that combines a lot of new features specific to SwiftUI.我正在学习 SwiftUI,为此,我正在开发一个小型应用程序,它结合了许多特定于 SwiftUI 的新功能。

However, I would like to create a UISwipeGestureRecognizer in order to be able to navigate in a UIViewRepresentable.但是,我想创建一个 UISwipeGestureRecognizer 以便能够在 UIViewRepresentable 中导航。

Basically, the user arrives on the main page and can either swipe to the left or to the right and bring him to the view in question.基本上,用户到达主页后,可以向左或向右滑动,将他带到相关视图。

Moreover, I just want to notice that I've done a lot of research, but I didn't see anything about SwipeGesture in SwiftUI (the AppleDocumentation is very short and doesn't show exemples for noobies as me !)此外,我只想注意到我已经做了很多研究,但是我没有看到 SwiftUI 中的 SwipeGesture 的任何内容(AppleDocumentation 非常短,并且没有显示像我这样的新手的示例!)

Here's my code for the moment:这是我目前的代码:


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
    }
}


Thank's for your help:)谢谢你的帮助:)

Gesture is a modifier on a view.手势是视图上的修饰符。 You can fake a swipe with a drag (you may want to add a time out for the gesture).您可以通过拖动来伪造滑动(您可能希望为手势添加超时)。 Note I had to expand your Text frame, otherwise the gesture only recognizes when you are on top of the Text:请注意,我必须扩展您的文本框架,否则手势仅在您位于文本顶部时才能识别:

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM