UIScrollView 阻止向一个方向滚动,无论 contentSize/Offset 是多少
我有一个启用了分页的 UIScrollView,但我希望能够阻止用户在某些情况下滑动到下一页或返回到上一页。
例如,某人可能通过了包含十个问题(每页一个问题)的测试,但除非该人回答了当前页面的问题,否则滚动将不起作用。
这也可能是为了阻止他们滑回到上一页。所以基本上我不能使用方向锁或暂时禁用用户交互,因为可以允许读者向左滑动但不能向右滑动。
我注意到有些人在玩内容偏移,但我发现该解决方案很难看,因为用户仍然可以滑动,只是应用程序自动重新调整内容偏移。我希望它就像视图已到达内容末尾或用户交互被禁用一样。
我想我也许可以“玩”内容大小/偏移并在每个页面上刷新它,但我不确定如何正确执行此操作,因为我也有“链接”可以允许某人从一个页面跳转到另一个页面(即当时可能超出定义的内容)。
我尝试过对锁定特定方向的滑动的页面使用自定义视图,但它不起作用,因为我找不到一种方法来阻止滚动视图检测滑动而不影响页面,滑动应该以正常方式工作。我看到我可以将自定义视图的 ExclusiveTouch 属性设置为 YES,并假设由于它将获取所有事件,所以我可以通过自定义手势识别器处理滑动,并在允许滑动的情况下转发事件,如果不允许则忽略它们。它不起作用,ScrollView 总是捕获滑动事件。
我觉得我已经用尽了所有的可能性,但我找不到任何有类似需求的人。
所以我想知道,这可能吗?如果是,是否需要糟糕的黑客或制作新的自定义滚动视图?
任何帮助/想法将不胜感激。
I have a UIScrollView with paging enabled but I would like to be able to prevent the user to swipe to the next page or back to the previous page in some cases.
For instance someone could be passing a test with ten questions (one question per page) but scrolling won't work unless the person has answered the question at the current page.
It could also be to prevent them to swipe back to the previous page. So basically I cannot use directional lock or temporarily disable user interaction because the reader could be allowed to swipe left but not to swipe right.
I noticed a few people play around with the content offset but I find that solution ugly because the user can still swipe, it's just that the app automatically readjusts the content offset. I would like it to be as if the view had reached the end of the content or if user interaction was disabled.
I guess I could maybe "play" with the content size/offset and refresh it on each page but I am not sure how to do that properly as I also have "links" that can allow someone to jump from one page to another (that could at the time be outside the defined content).
I have tried using a custom view for pages where swipe in a specific direction is locked but it doesn't work as I could not find a way to prevent the scroll view from detecting the swipe without affecting pages were swipe should work in a normal way. I saw that I could set that custom view exclusiveTouch property to YES and assumed that since it was going to get all the events I could handle swipes via custom gesture recognizers and forward the events if swipe was allowed or ignore them if not. It did not work, the ScrollView always catches swipe events.
I feel I have exhausted every possibility and I cannot find anyone who had similar needs.
So I am wondering, is this even possible? If yes, will it require bad hacks or to make a new custom scroll view?
Any help/idea would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码中您想要锁定滚动/分页的适当位置使用您的条件,并将您的scrollView对象的pagingEnabled或scrollEnabled设置为NO。
编辑:
应该更仔细地阅读您的要求。根据您的需求,更好的解决方案是根据您的条件使用多个视图从侧面滑入和滑出。将它们动画化。手势识别器也应该有所帮助。我这里说的不是scrollView。查看1->条件->通过? (按钮动作或手势)->动画进入view2等等。
如果你确实必须使用scrollView,最好的办法就是将contentSize设置为屏幕的宽度,并将alwaysBounceHorizontal设置为NO以防止自动弹跳效果:
use your condition at the appropriate place in your code where you want the scroll/paging locked and set pagingEnabled or scrollEnabled of your scrollView object to NO.
EDIT:
Should've read your requirement more carefully. For your needs a better solution would be to use multiple views slide in and out from the sides based on your conditionals. Animate them. Gesture recognizers should also help. And I'm not talking scrollView here. View1 -> condition -> passes? (button action or gesture) -> animation into view2 and so on.
If you really must use the scrollView, the best you can do is to set the contentSize to the width of the screen and set alwaysBounceHorizontal to NO to prevent the automatic bouncing effect:
在另一篇文章中发现,这就是我解决同样问题的方法。
将其放入视图控制器中并将其设置为 UIScrollView 的委托
Found in another post, this is how I solved the same problem I had.
Put this in you view controller and set it as the delegate of the UIScrollView