菜鸟教程小白 发表于 2022-12-11 18:30:03

ios - UIScrollView 的自定义分页


                                            <p><p>我有一个 UIScrollView 可以水平滚动并启用分页。有没有办法改变 ScrollView 的分页,使其在 ScrollView 的整个长度中捕捉到一半。我添加了显示我正在寻找的图片。</p>

<p> <a href="/image/CPogy.png" rel="noreferrer noopener nofollow"><img src="/image/CPogy.png" alt="Page 1"/></a> </p>

<p> <a href="/image/kHAEJ.png" rel="noreferrer noopener nofollow"><img src="/image/kHAEJ.png" alt="Page 2"/></a> </p>

<p> <a href="/image/LY4Ag.png" rel="noreferrer noopener nofollow"><img src="/image/LY4Ag.png" alt="Page 3"/></a> </p>

<pre><code>override func viewDidLoad() {
    super.viewDidLoad()


let scrollView = UIScrollView()
scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height)
scrollView.isPagingEnabled = true
self.view.addSubview(scrollView)

let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width / 2, height: self.view.frame.size.height)
view1.backgroundColor = UIColor.purple
scrollView.addSubview(view1)

let view2 = UIView()
view2.frame = CGRect(x: self.view.frame.size.width / 2, y: 0, width: self.view.frame.size.width / 2, height: self.view.frame.size.height)
view2.backgroundColor = UIColor.green
scrollView.addSubview(view2)

let view3 = UIView()
view3.frame = CGRect(x: self.view.frame.size.width, y: 0, width: self.view.frame.size.width / 2, height: self.view.frame.size.height)
view3.backgroundColor = UIColor.yellow
scrollView.addSubview(view3)

let view4 = UIView()
view4.frame = CGRect(x: self.view.frame.size.width + self.view.frame.size.width / 2, y: 0, width: self.view.frame.size.width / 2, height: self.view.frame.size.height)
view4.backgroundColor = UIColor.red
scrollView.addSubview(view4)

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以为 ScrollView 设置委托(delegate)并实现 <code>UIScrollViewDelegate</code> 方法之一:</p>

<pre><code>func scrollViewWillEndDragging(
    _ scrollView: UIScrollView,
    withVelocity velocity: CGPoint,
    targetContentOffset: UnsafeMutablePointer&lt;CGPoint&gt;
)
</code></pre>

<p>当您收到此调用时,您需要执行一些自定义逻辑。具体来说,您将检查 <code>targetContentOffset</code> 以及 <code>velocity</code> 以分别查看 scrollView 预期停止的位置以及用户拖动的方向。然后将 <code>targetContentOffset</code> 更改为它应该实际停止的新位置。</p>

<p>在您的情况下,新的 <code>targetContentOffset</code> 的 <code>x</code> 值将是偶数页的倍数。显然,如果您有奇数页,则必须考虑最后一页的偏移量并做一些不同的事情。</p>

<p>您可能还需要禁用默认的 <code>UIScrollView</code> 分页并自己处理所有分页,因为您的自定义行为与默认分页行为冲突。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIScrollView 的自定义分页,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41496659/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41496659/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIScrollView 的自定义分页