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

  1. 使用scrollToItem方法滚动集合视图
Swift 复制代码
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)
    }
}

上述代码中,首先使用scrollToItem方法将集合视图滚动到第一条数据(左侧对齐),然后在稍后的延迟时间后,再次使用scrollToItem方法将其滚动到最后一条数据(左侧对齐)。

  1. 使用setContentOffset方法来滚动集合视图
Swift 复制代码
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)
    }
}

上述代码中,我们使用了setContentOffset方法来滚动集合视图。我们获取了第一条数据和最后一条数据的布局属性,然后根据它们的位置计算出正确的contentOffset值,使得集合视图能够滚动到最后一条数据。

  1. 使用scrollRectToVisible方法进行滚动集合视图
Swift 复制代码
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)
    }
}

在上述代码中,我们使用了scrollRectToVisible方法来滚动集合视图。我们获取了第一条数据和最后一条数据的布局属性,并根据它们的位置计算出一个可见的矩形区域,然后将该矩形区域滚动到可见范围内。

相关推荐
云雀衔光10 分钟前
Java Spring AI MCP:在 Spring 里把 AI 接上你的业务系统
java·开发语言·人工智能·后端·测试工具·spring
安当加密030112 分钟前
PCI DSS 4.0与SWIFT CSP双合规:银行收单与跨境支付HSM部署实战
swift·hsm·密钥管理·pci dss·双合规
重生之小比特13 分钟前
【Java SE】数据类型与变量
java·开发语言·python
Yeniden16 分钟前
Java 后端从零到企业级:第1篇 Java 基础语法——变量、数据类型与运算符
java·开发语言·apache
luj_176827 分钟前
罚球线右移破防新策略
c语言·开发语言·网络·经验分享·算法
legendary_16339 分钟前
PD‑SINK芯片在无协议后端负载中的工程应用
c语言·开发语言·人工智能·智能手机·计算机外设
Nuanyt41 分钟前
JVM常见核心知识梳理02 类加载 字节码技术 双亲委派 Java内存模型 JMM 并发底层 volatile synchronized 常见排障与调优工具
java·开发语言·jvm
光影少年1 小时前
React18 对RN 的影响
android·前端·react.js·ios·前端框架
liliangcsdn1 小时前
IC计算-前向/远期收益计算和代码示例
开发语言·python·pandas
小溪学编程1 小时前
Java ListIterator 接口详解:双向遍历与列表修改的利器
java·开发语言·windows