就叫这个名 發表於 2023-7-27 10:55:57

Swift 如何让ScrollView滚动到具体某个位置

<p>1.&nbsp;使用<code>scrollToItem</code>方法滚动集合视图</p>
<div class="jb51code"><pre class="brush:java;">DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    let firstIndexPath = IndexPath(item: 0, section: 0)
    let lastIndexPath = IndexPath(item: self.recordArray.count - 1, section: 0)
    // Scroll to first item
    self.collectionView.scrollToItem(at: firstIndexPath, at: .left, animated: false)
    // Delay for a short time (e.g., 0.1 seconds)
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
      // Scroll to last item
      self.collectionView.scrollToItem(at: lastIndexPath, at: .left, animated: false)
    }
}</pre></div>
<p>上述代码中,首先使用<code>scrollToItem</code>方法将集合视图滚动到第一条数据(左侧对齐),然后在稍后的延迟时间后,再次使用<code>scrollToItem</code>方法将其滚动到最后一条数据(左侧对齐)。</p>
<p>2. 使用<code>setContentOffset</code>方法来滚动集合视图</p>
<div class="jb51code"><pre class="brush:java;">DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    let firstIndexPath = IndexPath(item: 0, section: 0)
    let lastIndexPath = IndexPath(item: self.recordArray.count - 1, section: 0)
    if let firstCellAttributes = self.collectionView.layoutAttributesForItem(at: firstIndexPath),
       let lastCellAttributes = self.collectionView.layoutAttributesForItem(at: lastIndexPath) {
      let contentOffset = CGPoint(x: lastCellAttributes.frame.origin.x - firstCellAttributes.frame.origin.x,
                                    y: 0)
      self.collectionView.setContentOffset(contentOffset, animated: false)
    }
}</pre></div>
<p>上述代码中,我们使用了<code>setContentOffset</code>方法来滚动集合视图。我们获取了第一条数据和最后一条数据的布局属性,然后根据它们的位置计算出正确的<code>contentOffset</code>值,使得集合视图能够滚动到最后一条数据。</p>
<p>3. 使用<code>scrollRectToVisible</code>方法进行滚动集合视图</p>
<div class="jb51code"><pre class="brush:java;">DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    let firstIndexPath = IndexPath(item: 0, section: 0)
    let lastIndexPath = IndexPath(item: self.recordArray.count - 1, section: 0)
    if let firstCellAttributes = self.collectionView.layoutAttributesForItem(at: firstIndexPath),
       let lastCellAttributes = self.collectionView.layoutAttributesForItem(at: lastIndexPath) {
      let firstRect = firstCellAttributes.frame
      let lastRect = lastCellAttributes.frame
      let visibleRect = CGRect(x: lastRect.origin.x, y: 0, width: self.collectionView.bounds.width, height: self.collectionView.bounds.height)
      self.collectionView.scrollRectToVisible(visibleRect, animated: false)
    }
}</pre></div>
<p>在上述代码中,我们使用了<code>scrollRectToVisible</code>方法来滚动集合视图。我们获取了第一条数据和最后一条数据的布局属性,并根据它们的位置计算出一个可见的矩形区域,然后将该矩形区域滚动到可见范围内。</p>
<p>到此这篇关于Swift 让ScrollView滚动到具体某个位置的文章就介绍到这了,更多相关Swift ScrollView设置滚动位置内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>iOS ScrollView实现自动布局的方法(适用Swift 3.0 )</li><li>swift实现自动轮播图效果(UIScrollView+UIPageControl+Timer)</li><li>Swift实现可自定义分页宽度的UIScrollView</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Swift 如何让ScrollView滚动到具体某个位置