Swift 如何讓ScrollView滾動(dòng)到具體某個(gè)位置
1. 使用scrollToItem
方法滾動(dòng)集合視圖
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
方法將集合視圖滾動(dòng)到第一條數(shù)據(jù)(左側(cè)對(duì)齊),然后在稍后的延遲時(shí)間后,再次使用scrollToItem
方法將其滾動(dòng)到最后一條數(shù)據(jù)(左側(cè)對(duì)齊)。
2. 使用setContentOffset
方法來(lái)滾動(dòng)集合視圖
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
方法來(lái)滾動(dòng)集合視圖。我們獲取了第一條數(shù)據(jù)和最后一條數(shù)據(jù)的布局屬性,然后根據(jù)它們的位置計(jì)算出正確的contentOffset
值,使得集合視圖能夠滾動(dòng)到最后一條數(shù)據(jù)。
3. 使用scrollRectToVisible
方法進(jìn)行滾動(dòng)集合視圖
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
方法來(lái)滾動(dòng)集合視圖。我們獲取了第一條數(shù)據(jù)和最后一條數(shù)據(jù)的布局屬性,并根據(jù)它們的位置計(jì)算出一個(gè)可見(jiàn)的矩形區(qū)域,然后將該矩形區(qū)域滾動(dòng)到可見(jiàn)范圍內(nèi)。
到此這篇關(guān)于Swift 讓ScrollView滾動(dòng)到具體某個(gè)位置的文章就介紹到這了,更多相關(guān)Swift ScrollView設(shè)置滾動(dòng)位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Swift中優(yōu)雅處理閉包導(dǎo)致的循環(huán)引用詳解
這篇文章主要給大家介紹了關(guān)于Swift中優(yōu)雅的處理閉包導(dǎo)致的循環(huán)引用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08關(guān)于Swift 4.1中的Codable改進(jìn)詳解
這篇文章主要給大家介紹了關(guān)于Swift 4.1中的Codable改進(jìn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02Swift map和filter函數(shù)原型基礎(chǔ)示例
這篇文章主要為大家介紹了Swift map和filter函數(shù)原型基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Ubuntu 16.04上安裝 Swift 3.0及問(wèn)題解答
本文給大家分享的是在Ubuntu系統(tǒng)中安裝 Swift 3.0的方法和步驟,以及安裝過(guò)程中有可能遇到的問(wèn)題的解答,這里推薦給小伙伴們,希望大家能夠喜歡2016-07-07Swift中使用可選類(lèi)型完美解決占位問(wèn)題
這篇文章主要介紹了Swift中使用可選類(lèi)型完美解決占位問(wèn)題,本文講解了為Dictionary增加objectsForKeys函數(shù)、Swift中更簡(jiǎn)便的方法、內(nèi)嵌可選類(lèi)型等內(nèi)容,需要的朋友可以參考下2015-05-05淺談Swift編程中switch與fallthrough語(yǔ)句的使用
這篇文章主要介紹了Swift編程中switch與fallthrough語(yǔ)句的使用,用于基本的流程控制,需要的朋友可以參考下2015-11-11Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用
這篇文章主要給大家介紹了關(guān)于Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Swift SQLite具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04如何在Swift?中使用?async?let?并發(fā)運(yùn)行后臺(tái)任務(wù)
Swift?異步編程是一種編寫(xiě)允許某些任務(wù)并發(fā)運(yùn)行而不是按順序運(yùn)行的代碼的方法,這篇文章主要介紹了在Swift中使用async?let并發(fā)運(yùn)行后臺(tái)任務(wù),需要的朋友可以參考下2023-06-06深入探究Swift枚舉關(guān)聯(lián)值的內(nèi)存
這篇文章主要給大家介紹了關(guān)于Swift枚舉關(guān)聯(lián)值的內(nèi)存的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08