gpt4 book ai didi

ios - 在 UICollectionView 的最后一项上呈现 View Controller

转载 作者:行者123 更新时间:2023-11-29 01:19:42 25 4
gpt4 key购买 nike

我正在制作简单的聊天应用程序,因此当用户点击聊天列表中的特定聊天时,他必须转到该特定聊天室中的最后一条消息(如 Telegram 中)。我尝试创建自己的 scrollToBottom 函数:

func scrollToBottom(animated animated: Bool) {

if self.collectionView.numberOfSections() == 0 {
return
}

if self.collectionView.numberOfItemsInSection(0) == 0 {
return
}

let collectionViewContentHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height
let isContentTooSmall = collectionViewContentHeight < CGRectGetHeight(self.collectionView.bounds)

if isContentTooSmall {

self.collectionView.scrollRectToVisible(CGRectMake(0, collectionViewContentHeight - 1, 1, 1), animated: animated)
return
}

let finalRow = max(0, self.collectionView.numberOfItemsInSection(0) - 1)
let finalIndexPath = NSIndexPath(forRow: finalRow, inSection: 0)
let finalCellSize = self.collectionView.messagesCollectionViewLayout.sizeForItem(finalIndexPath)

let maxHeightForVisibleMessage = self.collectionView.bounds.height - self.collectionView.contentInset.top
let scrollPosition: UICollectionViewScrollPosition = finalCellSize.height > maxHeightForVisibleMessage ? .Bottom : .Top

self.collectionView.scrollToItemAtIndexPath(finalIndexPath, atScrollPosition: scrollPosition, animated: animated)
}

但是,当我在 viewWillAppear 中调用它时,在它跳转到聊天室的最后一行之前,我有一点延迟,这并不好。请帮忙!

最佳答案

viewWillAppear 将在 viewDidLoad 之后调用。如果您需要在 viewcontroller 启动后调用此方法,则可以在 viewDidload 方法中添加代码。但如果你仍然需要使用 viewWillAppear,我建议将这个调用包装在主线程或 mainqueue 上。您可以为此使用 GCD 或 NSOperationQueue。

我认为这可能有助于解决延迟问题

关于ios - 在 UICollectionView 的最后一项上呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34763148/

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