gpt4 book ai didi

ios - UIScrollView:水平分页,垂直滚动?

转载 作者:行者123 更新时间:2023-12-01 16:51:35 25 4
gpt4 key购买 nike

如何强制 UIScrollView在哪个分页和滚动中仅在给定时刻垂直或水平移动?

我的理解是 directionalLockEnabled属性应该实现这一点,但对角滑动仍然会导致 View 对角滚动,而不是将运动限制在单个轴上。

编辑:为了更清楚,我想允许用户水平或垂直滚动​​,但不能同时滚动。

最佳答案

为了将来引用,我以 Andrey Tanasov 的回答为基础,提出了以下可以正常工作的解决方案。

首先定义一个变量来存储 contentOffset 并将其设置为 0,0 坐标

var positionScroll = CGPointMake(0, 0)

现在在 scrollView 委托(delegate)中实现以下内容:
override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
// Note that we are getting a reference to self.scrollView and not the scrollView referenced passed by the method
// For some reason, not doing this fails to get the logic to work
self.positionScroll = (self.scrollView?.contentOffset)!
}

override func scrollViewDidScroll(scrollView: UIScrollView) {

// Check that contentOffset is not nil
// We do this because the scrollViewDidScroll method is called on viewDidLoad
if self.scrollView?.contentOffset != nil {

// The user wants to scroll on the X axis
if self.scrollView?.contentOffset.x > self.positionScroll.x || self.scrollView?.contentOffset.x < self.positionScroll.x {

// Reset the Y position of the scrollView to what it was BEFORE scrolling started
self.scrollView?.contentOffset = CGPointMake((self.scrollView?.contentOffset.x)!, self.positionScroll.y);
self.scrollView?.pagingEnabled = true
}

// The user wants to scroll on the Y axis
else {
// Reset the X position of the scrollView to what it was BEFORE scrolling started
self.scrollView?.contentOffset = CGPointMake(self.positionScroll.x, (self.scrollView?.contentOffset.y)!);
self.collectionView?.pagingEnabled = false
}

}

}

希望这可以帮助。

关于ios - UIScrollView:水平分页,垂直滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/728014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com