UIScrollView 单向分页

UIScrollView paging in single direction(UIScrollView 单向分页)
本文介绍了UIScrollView 单向分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

可能重复:
如何为 UIScrollView 启用方向锁定?

有没有办法让 UIScrollView 执行分页,例如仅水平滚动而不是垂直滚动?

Is there a way to have a UIScrollView perform paging e.g. only horizontally and not when scrolling vertically?

换句话说,我希望我的滚动视图在水平滚动时捕捉到页面边界,并在垂直滚动时自由滑动.

In other words, I want my scrollview to snap to page boundries when scrolling horizontally and free swiping when going vertically.

当然,简单的解决方案是使用两个 UIScrollView,每个方向一个,但在我的情况下这不是一个可行的解决方案.

Of course the trivial solution is using two UIScrollViews, one in each direction, but this is not a feasible solution in my case.

推荐答案

假设你总是想要在水平方向有任何移动时捕捉,并且只有在水平偏移没有改变的情况下才能自由滚动.

Presuming you always want snap if there is any movement in horizontal direction and free scrolling only if the horizontal offset has not changed.

  1. 设置 directionalLockEnabled = YES
  2. 将 UIScrollView 包裹在另一个向前触摸的视图中(因为我们将修改滚动视图的框架)
  3. 将 UIScrollView 的框架高度设置为 1(以便在处理水平滚动时最大限度地减少垂直对齐)
  4. 检测当前滑动的方向(在 scrollViewDidScroll: 中)
  5. 如果 contentOffset 的 x 发生了变化,则设置 pagingEnabled = YES
  6. 如果只有 y 的 contentOffset 发生了变化,则设置 pagingEnabled = NO

这将模拟您想要的场景.如果您水平滑动页面将捕捉 - 由于成名高度设置为 1,垂直捕捉将最小化.
仅垂直滑动时,滚动视图将自由滚动.

This will simulate the scenario you want. If you swipe horizontally the page will snap - and the vertical snap will be minimal due to the fame height being set to 1.
When swiping only vertically, the scrollview will scroll freely.

这篇关于UIScrollView 单向分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
Resume game cocos2d(恢复游戏 cocos2d)
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)