简体   繁体   English

UICollectionView中的单边方向滚动?

[英]Unilateral Direction Scrolling in a UICollectionView?

I am trying to get a UICollectionView to scroll either completely vertically or completely horizontally, depending on the direction of the swipe/pan. 我试图让UICollectionView完全垂直或完全水平滚动,具体取决于滑动/平移的方向。

There should be no diagonal scrolling. 不应有对角滚动。

If a user swipes in a diagonal direction, it should choose the stronger direction, either vertical or horizontal, and then scroll in only that direction. 如果用户在对角线方向上滑动,则应选择更强的方向(垂直或水平),然后仅在该方向上滚动。

The other answers on the web are related to using a CollectionViewFlowLayout , which allows a single direction. 网络上的其他答案与使用CollectionViewFlowLayout ,后者允许一个方向。 But, I had to subclass the UICollectionViewLayout in order to allow both horizontal and vertical scrolling. 但是,我必须对UICollectionViewLayout进行子类UICollectionViewLayout ,以允许水平和垂直滚动。

Now, I want to disable the diagonal scrolling, but keep the ability to horizontally/vertically scroll. 现在,我想禁用对角滚动,但保持水平/垂直滚动的能力。

I have already used the collectionView.isDirectionLockEnabled , but according to apple Docs: 我已经使用过collectionView.isDirectionLockEnabled ,但是根据苹果的Docs:

If this property is true and the user begins dragging in one general direction (horizontally or vertically), the scroll view disables scrolling in the other direction. 如果此属性为true,并且用户开始在一个大致方向(水平或垂直)上拖动,则滚动视图将禁止在另一个方向上滚动。 If the drag direction is diagonal, then scrolling will not be locked and the user can drag in any direction until the drag completes. 如果拖动方向是对角线,则滚动不会被锁定,并且用户可以向任意方向拖动,直到拖动完成。

It still allows diagonal scrolling, hence not very helpful to my desired goal. 它仍然允许对角滚动,因此对我想要的目标不是很有帮助。

I am looking for some sort of override point where I could catch the direction of the drag/scroll, and then only allow a certain direction to be scrolled. 我正在寻找某种替代点,可以在其中捕获拖动/滚动的方向,然后只允许滚动某个方向。

EDIT: Here is my relevant code. 编辑:这是我的相关代码。 This code has made it harder to scroll diagonally, but on occasion, I can still get it to scroll diagonally: 这段代码使对角滚动变得更加困难,但是有时候,我仍然可以对角滚动:

var initialContentOffset: CGPoint = CGPoint.zero

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    initialContentOffset = scrollView.contentOffset
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.isDragging {
        let velocity = scrollView.panGestureRecognizer.velocity(in: scrollView)

        if abs(velocity.y) > abs(velocity.x) {
            scrollView.contentOffset = CGPoint(x: initialContentOffset.x, y: scrollView.contentOffset.y)
        } else if abs(velocity.x) > abs(velocity.y) {
            scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: initialContentOffset.y)
        }
    }
}

You should do a combination of checking against multiple delegates of the scrollView . 您应该结合对scrollView多个delegates进行检查。 Check direction of scroll in UIScrollView 在UIScrollView中检查滚动方向

1: you need to keep a Boolean property isScrolling to know if the scrollView is scrolling or if it has stopped , and probably a property to keep a reference to which direction the user is currently scrolling if (isScrolling == YES) 1:您需要保留一个布尔属性isScrolling以了解scrollView 是否正在滚动或是否已停止如果 (isScrolling == YES) ,则可能需要一个属性来保持对用户当前滚动方向的引用

2: Now in this method: 2:现在使用这种方法:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

We need to check what direction the user will scroll and at the same time , you can do a check against the first flag isScrolling : 我们需要检查用户将滚动的方向,同时,您可以检查第一个标志isScrolling

  • if the scrollView has stopped scrolling, you allow the scrolling to begin and set the isScrolling flag to YES and only allow the scrolling in one direction. 如果scrollView停止滚动,则允许滚动开始并将isScrolling标志设置为YES 并且仅允许在一个方向上滚动。
  • if the scrollView has not stopped scrolling, you simply set the scrollView.contentOffset x or y , depending on which direction you want to allow the scrollView to keep scrolling. 如果scrollView尚未停止滚动,则只需设置scrollView.contentOffset xy ,具体取决于要允许scrollView继续滚动的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM