0

I have a UIPageViewController with four tabs. Three tables and one webview. The problem is that vertical scrolling does not work on them. How can I solve this using swift?

This is my code:

func createTxtPageViewController() {

        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("pagetxtController") as! UIPageViewController
        pageController.dataSource = self
        pageController.delegate = self

        if texts.count > 0 {
            let firstController = getItemTxtController(0)!
            let startingViewControllers = [firstController]
            pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }

        pageTxtViewController = pageController
        pageTxtViewController?.view.frame = CGRectMake(10, 173, self.rightView.frame.size.width - 20, self.rightView.frame.size.height - 183)

        addChildViewController(pageTxtViewController!)
        self.rightView.addSubview(pageTxtViewController!.view)
        pageTxtViewController!.didMoveToParentViewController(self)
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
breno
  • 230
  • 2
  • 15

1 Answers1

-1

If you have added a PanGestureRecognizer it's try to override tableView's gesture recognizer you have to do a manual check using UIGestureRecognizerDelegate.

let pangGesture = UIPanGestureRecognizer(target: self, action: #selector(createTxtPageViewController(recognizer:)))
pangGesture.delegate = self
self.view.addGestureRecognizer(pangGesture)

then you have to implement the gestureRecongizerShouldBegin delegate function

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

    // we check for panGesture if it's panGesture we continue if not return        
    guard let recognizer = gestureRecognizer as? UIPanGestureRecognizer  else {
        return true
    }

    // The velocity of the pan gesture in the coordinate system of the specified view. 
    let velocity = recognizer.velocity(in: self.view)

    // If velocity.y is greater than velocity.x user is trying to scroll tableView else user wants to swipe to next screen
    return abs(velocity.y) > abs(velocity.x) ? false : true
}

declare a CGPoint property at the top of your class for touchLocation

var panStartPoint: CGPoint!

Now inside your createTxtPageViewController function check for the states and detect users swipe direction left or right

func createTxtPageViewController(recognizer: UIPanGestureRecognizer) {


    switch recognizer.state {
    case .began:
        touchLocation = recognizer.location(in: self.view)
    case .changed:
        let currentPoint = recognizer.location(in: self.view)
        let swipingLeft = currentPoint.x < touchLocation.x ? true : false

        if !swipingLeft {
            // swiped right
            //TODO: Add your logic for navigation to nextViewController
        } else {
            // swiped left
            //TODO: Add your logic for navigation to nextViewController
        }
        // you can check for other states as well and clean up after your self e.g state.cancelled I just break
    default:
        break
    }

}
Khalid Afridi
  • 913
  • 5
  • 12
  • Sorry me but I did not understand. I'm going to replace my UIPageViewController with Gesture? – breno May 10 '17 at 12:31
  • I think you already have swipe gesture in your UIPageViewController? – Khalid Afridi May 10 '17 at 12:42
  • Yes I have it and it works. I did not understand why redo my "createTxtPageViewController" method that is used for a UIPageViewController creation. – breno May 10 '17 at 12:43
  • So I understand I will manually recreate the gesture that runs my UIPageViewController because this is replacing the scroll of the tableView. Making the table scrolls normally. It is? – breno May 10 '17 at 12:46
  • here is the answer how to disable UIPageViews swipe [How do i Disable the swipe] (http://stackoverflow.com/questions/22098493/how-do-i-disable-the-swipe-gesture-of-uipageviewcontroller) – Khalid Afridi May 10 '17 at 12:51
  • I try this but not work. The problem is my content view. My UIPageViewController is working. Table scroll inside my UIPageViewController not work – breno May 10 '17 at 13:35