Prevent UIPageViewController's swipe from affecting a UISlider

我是研究僧i 提交于 2019-12-11 02:29:37

问题


I've seen this question asked in other areas but none of the answers work in my case. I have a UIPageViewController that has a child view controller containing a UISlider.

When you go to try to move the slider, it activates the page view controller's scrolling instead. You have to tap, pause on the slider circle, and then move.

The UIPageViewController is set to the scroll type, and thus the gestureRecognizers are not set. (The header states "Only populated if transition style is 'UIPageViewControllerTransitionStylePageCurl'"). Thus I can't even get proper access to the gestures.

I've tried several things to prevent the UIPageViewController from affecting the slider, but to no avail. I've tried: 1) walking the child subtree and setting the UIScrollView's pan gesture delegate to myself (this causes a crash) 2) Add my own gesture on top, and check the y value. This will prevent the swipe, but it also eats the tap and move so the slider doesn't actually change as expected 3) Implemented a custom slider with custom gestures. The problem is I can't match the pan gesture changing the slider in the same was as the default. Since you have to implement the pan and adjust the positions yourself, it's difficult to match the real behavior.

None of these methods actually works for me. Has anyone been able to solve this? The only solution I can think of is to prevent scrolling all together, add left/right buttons, and programmatically transition pages accordingly.


回答1:


You can add an UIPanGestureRecognizer to the slider with the cancelsTouchesInView attribute to false to avoid the UIPageViewController to swipe when you drag the slider :

let panGesture = UIPanGestureRecognizer()
panGesture.cancelsTouchesInView = false
yourSlider.addGestureRecognizer(panGesture)



回答2:


I think the best solution is to redesign the UI to not have components that are horizontally scrolled/swiped on a UIPageViewController. The user will already be used to performing horizontal swipes anywhere on the UIPageViewController and it could be confusing/frustrating if that behavior is changed.



来源:https://stackoverflow.com/questions/33951748/prevent-uipageviewcontrollers-swipe-from-affecting-a-uislider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!