UIScrollView单向调页 [英] UIScrollView paging in single direction

查看:133
本文介绍了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.

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

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包装在转发触摸的另一个视图中(因为我们将修改scrollview的fram`e)

  3. 将UIScrollView的框架高度设置为1(以便在处理水平滚动时最小化垂直对齐)

  4. 检测当前滑动的方向(在中scrollViewDidScroll:

  5. 如果contentOffset的x有更改,然后设置pagingEnabled = YES

  6. 如果只更改了contentOffset的y,则设置pagingEnabled = NO

  1. Set directionalLockEnabled = YES
  2. Wrap the UIScrollView in another view which forwards touches (as we will modify the scrollview's fram`e)
  3. Set the UIScrollView's frame height to 1 (in order to minimize vertical snapping when handling horizontal scroll)
  4. Detect the direction of the current swipe (in scrollViewDidScroll:)
  5. If x of contentOffset has changed, then set pagingEnabled = YES
  6. If only y of contentOffset has changed, then set 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单向调页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆