ALISON
ALISON

Reputation: 5

Managing UIPageViewController with ScrollView is not loading Next/Previous contents

I have a 3 view controllers in my UIPageViewController. I am accesing the UIPageViewController's scroll view from one of my view controller which is in UIPageViewController and managing it with a UIPanGesture. I am always in mid controller and The problem is the content is in next or previous never gets shown if I don't swipe via UIPageViewController. I see the background of the UIPageViewController. Logic works, everything in main thread but for somehow i need to say UIPageViewController hey prepare the next data i am coming with UIPanGesture. I noticed When i swipe a little bit in my UIPageViewController it's firing the viewController after and viewcontroller before methods. Here is the code

The code I am sending how much it should scroll the UIPageViewController from my Controller :

    @objc func panRecognized(gesture: UIPanGestureRecognizer) {
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            
            switch overflowState {
            case .on(let lastRecordedGestureTranslation):
                if gesture.state == .ended || gesture.state == .possible {
                    self.viewModel.chatScrollDetectedSwipeDirection = nil
                    self.viewModel.objectToMove = nil
                    self.viewModel.chatScrollViewPosition = self.checkTableViewPosition(chatTableView)
                    self.viewModel.baseToControllerDelegate?.onOverflowEnded()
                    overflowState = .off
                    chatTableScrollView?.panGestureRecognizer.isEnabled = true
                    self.viewModel.baseToControllerDelegate?.pageControllerSetContentOffsets(prog: 0)
                    return
                }
                

//This is where i am calling hey page controller start moving
                let fullTranslationY = gesture.translation(in: view).y
                let delta = fullTranslationY - lastRecordedGestureTranslation
                overflowState = .on(lastRecordedGestureTranslation: fullTranslationY)
                
                self.viewModel.baseToControllerDelegate?.onOverflow(delta: delta)
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true // so that both the pan gestures on scrollview will be triggered
    }

The code to move UIPageViewController from one my page

    func onOverflow(delta: CGFloat) {
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            guard let scrollViewA = self.pageViewController?.view.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView else { return }
            scrollViewA.contentOffset.y -= delta / 2
        }
    }

Thank you!

**Things I tried : ** Manually firing viewControllerBefore and viewControllerAfter (Did not work) ** Things i did not understand : **: If i move in my controllers view setViewController animated true and false. It works but creates a glitch view that my controllers are swiping 2 times untill i reach my mid controller.

Upvotes: 0

Views: 65

Answers (0)

Related Questions