问题
I need to add a UIPageViewController inside UIScrollView.
My view hierarchy is UIVIewController (UIView) > UIScrollVIew > UIVIew(ContentView)
And inside that, I added my UIPageViewController programmatically.
UIPageViewController is loaded perfectly as expected. Scrollview is scrolling as well. But its page gestures got disabled by I guess scrollview.
Basically, i need UIPageView inside scrollview. I want the scrollview to scroll only vertically. And horizontal swipes should only work for uipageviewcontroller for page changing. Basically like book reader app in landscape mode. I am also using this screen to work only on landscape view.
Here is my code
class QuranLandController: UIViewController {
@IBOutlet weak var quranContainer: UIView!
@IBOutlet weak var scrollView: UIScrollView!
// UIPageViewController
let controller = QuranPagerController()
let imgWidth = 2048.0
let imgHeight = 2732.0
override func viewDidLoad() {
super.viewDidLoad()
let h = CGFloat((imgHeight/imgWidth) * Double(self.view.frame.width))
// quranContainer.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: h)
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.didMove(toParentViewController: self)
// Translator().defaultTranslation()
for recognizer in controller.gestureRecognizers {
if recognizer is UITapGestureRecognizer {
recognizer.isEnabled = false
}
}
scrollView.canCancelContentTouches = false
scrollView.delaysContentTouches = false
scrollView.isUserInteractionEnabled = true
scrollView.isExclusiveTouch = true
scrollView.isDirectionalLockEnabled = true
// scrollView.isScrollEnabled = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let h = CGFloat((imgHeight/imgWidth) * Double(self.view.frame.width))
quranContainer.layout(controller.view)
.width().top().left().right().height(h)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: h)
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight
}
override var shouldAutorotate: Bool {
return true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
来源:https://stackoverflow.com/questions/60484090/uipageview-page-turn-gestures-got-disabled-when-used-inside-uiscrollview