3

私はUICollectionView142個のセルを持っています。7.5 はいつでも表示されます。

セルをindexPath0 から 100 に移動しています。しかし、その新しい位置までスクロールしたいのです。

以下のコードは正常に動作します。ただし、移動とスクロールをアニメーション化しますが、その後、中央/移動されたセルの前後にセルをロードします。細胞が再利用できるからだと思います。しかし 142 では、すべてをプリロードすることはできません。

100 の前に 4 つ、後の 4 つの新しい位置を囲むセルをプリロードしindexPath、移動とスクロールのアニメーションを確認したいと思います。お願いできますか?

UIView.animateWithDuration(animationSpeed, animations: {() -> Void in
    self.collectionView.layoutIfNeeded()
    self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem:self.indexPathSelected.row, 
                                                 inSection:0), 
                                                 atScrollPosition: .CenteredHorizontally, 
                                                 animated: true)

})
4

6 に答える 6

7

dispatch_async を使用して UI を更新すると、機能します。

let numberOfSections = self.collectionView.numberOfSections()
let numberOfRows = self.collectionView.numberOfItemsInSection(numberOfSections-1)

    if numberOfRows > 0 {
        dispatch_async(dispatch_get_main_queue(), {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
        })
    }
于 2016-04-25T05:44:25.280 に答える
0

@Polittaの答えは正しいです。Objective C で必要なものを実装する方法は次のとおりです。

- (void)scrollToBottomWithoutAnimation{
    NSInteger lastVisibleRowIndex = (NSInteger)self.myItems.count - 1;
    NSIndexPath *lastVisibleIndexPath = [NSIndexPath indexPathForRow: lastVisibleRowIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:lastVisibleIndexPath atScrollPosition: UICollectionViewScrollPositionCenteredVertically animated:NO];
}

</p>

あなたのviewWillAppearメソッドで:

dispatch_async(dispatch_get_main_queue(), ^{
    [self scrollToBottomWithoutAnimation];
});
于 2020-08-31T08:46:56.743 に答える
0

[self.view layoutIfNeeded]電話をかける前に電話する必要がありますscrollToBottomWithoutAnimation

于 2021-09-07T14:32:01.047 に答える
0

私が見つけた最良の解決策は、を使用することでしたDispatchQueue

スウィフト 4.2:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let index = items.firstIndex(of: preSelectedItem) {
        DispatchQueue.main.async {
            self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .top, animated: false)
        }
    }
}
于 2018-11-06T21:37:53.890 に答える