Ironnstarz
Ironnstarz

Reputation: 81

SwiftUI : How to implement SwipGesture?

I would like to create a UISwipeGestureRecognizer in order to be able to navigate in a 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 Apple documentation is very short and doesn't show examples)

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

Upvotes: 3

Views: 1824

Answers (1)

Josh Homann
Josh Homann

Reputation: 16327

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

Upvotes: 2

Related Questions