swiftuiscrollviewuitextviewuigesturerecognizeruipageviewcontroller

UITextView scrolling without triggering UIPageViewControllerDataSource in UIPageViewController's view controllers


I have multiple view controllers. Something like this:

ViewControllerA ViewControllerB ViewControllerC

I use these as my UIPageViewController's view controllers, (as pages).

Each one of them has scrollable UITextViews inside. I cannot disable scrolling because text length should be flexible.

When I scroll the textview, UIPageViewControllerDataSource's viewControllerBefore or viewControllerAfter also is being triggered. How can I prevent this?

Can I disable vertical gestures for UIPageViewController and prevent clashes? Or is there some other way to stop them working at the same time? I want to change page only on horizontal gesture, and scroll the text view only on vertical gesture. But I don't know how to do it. What should I do?


Solution

  • Edit: Thank you @DonMag and @BulatYakupov for trying to help me and warning me about the given information is not enough. I had some restriction on code sharing. Today I found another way. But first here how my screen looks :

    enter image description here

    I was swiping to turn the page with page curl.

    When I scroll the text, it was also triggering viewControllerBefore or viewControllerAfter. It wasn't changing the page, but It was triggering my view controller creating function. This function keep track of current index on an array to supply data to vc creation.

    It is something like this:

    class myCustomVC: UIViewController {
        let textView = UITextView()
    }
    
    class PageVCDataService {
        var array = ["John","Josh","Joe","Harvy"]
        //keeping track of the current index for suppling correct text from array for every page
        var currentIndex = 0
    
        func createAfterPage() -> UIViewController {
            currentIndex += 1
            return createVC(page :array[currentIndex])
        }
        
        func createBeforePage() -> UIViewController {
            currentIndex -= 1
            return createVC(page :array[currentIndex])
        }
    
        func createVC(page : String) ->UIViewController {
            let vc = myCustomVC()
            vc.textView.text = page
            return vc
        }
    }
    
    class PageVC : UIPageViewController {
        let dataService = PageVCDataService()
        // ...some code
    }
    extension PageVC: UIPageViewControllerDataSource {
        
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            return dataService.createBeforePage()
        }
        
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            return dataService.createAfterPage()
        }
    }
    
    

    It wasn't changing the page but it was changing the index. For example if I scroll the text and if my current index at 10 (page 11), my current index was changing to 9. When I swipe for next page, next page was the same page because index was going 9 to 10, which is the same index at the beginning.

    So i implemented UITextViewDelegate and used scrollViewDidScroll method. Every time text view scrolls, it send a notification to my data service class, tell it to check if the text on the screen and the text at the current index is same. If it's not, my function find the index for the text on the screen and corrects current index.

    It is an ugly solution but maybe it can help someone else.