简体   繁体   English

如何以编程方式滚动浏览集合视图?

[英]How to programmatically scroll through a collection view?

I have a collection view in my view.我有一个集合视图。 In the cells are image views and it has one section.在单元格中是图像视图,它有一个部分。

I now want to scroll programmatically through the images.我现在想以编程方式滚动浏览图像。 I want the animation to happen endless so it starts with item one after reaching item 10.我希望动画无休止地发生,所以它在到达第 10 项后从第 1 项开始。

It also would be helpful if you provide some method where to put animations like the cells getting bigger after getting programmatically swiped in from right.如果您提供一些方法来放置动画,例如在以编程方式从右侧滑入后,单元格会变大,这也会很有帮助。

There are two methods that could help you achieve this:有两种方法可以帮助您实现这一目标:

func scrollToItemAtIndexPath(indexPath: NSIndexPath,
        atScrollPosition scrollPosition: UICollectionViewScrollPosition,
                animated animated: Bool)

or

func setContentOffset(contentOffset: CGPoint,
         animated animated: Bool)

Both are on UICollectionView so you can use whatever seems more convenient.两者都在UICollectionView因此您可以使用看起来更方便的任何东西。 To create a custom animation for this however is more difficult.然而,为此创建自定义动画更加困难。 A quick solution depending on what you need could be this answer .根据您的需要,快速解决方案可能是这个答案

Swift 4, iOS 11:斯威夫特 4,iOS 11:

// UICollectionView method
func scrollToItem(at indexPath: IndexPath, 
                  at scrollPosition: UICollectionViewScrollPosition,  
                  animated: Bool)

// UIScrollView method
func setContentOffset(_ contentOffset: CGPoint, animated: Bool)

Swift 5斯威夫特 5

Based on Mr.Bean's answer , here is an elegant way using UICollectionView extension:基于Mr.Bean 的回答,这是使用 UICollectionView 扩展的一种优雅方式:

extension UICollectionView {
    func scrollToNextItem() {
        let contentOffset = CGFloat(floor(self.contentOffset.x + self.bounds.size.width))
        self.moveToFrame(contentOffset: contentOffset)
    }

    func scrollToPreviousItem() {
        let contentOffset = CGFloat(floor(self.contentOffset.x - self.bounds.size.width))
        self.moveToFrame(contentOffset: contentOffset)
    }

    func moveToFrame(contentOffset : CGFloat) {
        self.setContentOffset(CGPoint(x: contentOffset, y: self.contentOffset.y), animated: true)
    }
}

Now you can use it wherever you want:现在你可以在任何你想要的地方使用它:

collectionView.scrollToNextItem()
collectionView.scrollToPreviousItem()

By far this is the best approach i have encountered, the trick is to scroll to the frame which is containing next objects.到目前为止,这是我遇到的最好的方法,诀窍是滚动到包含下一个对象的框架。 Please refer the code请参考代码

/* -------------- display previous friends action ----------------*/
@IBAction func actionPreviousFriends(_ sender: Any) {

    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
    self.moveToFrame(contentOffset: contentOffset)
}

/* -------------- display next friends action ----------------*/
@IBAction func actionNextFriends(_ sender: Any) {

    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
    self.moveToFrame(contentOffset: contentOffset)
}

func moveToFrame(contentOffset : CGFloat) {

    let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
    self.collectionView.scrollRectToVisible(frame, animated: true)
}

You can use this UICollectionView extension ( Swift 4.2 )你可以使用这个 UICollectionView 扩展( Swift 4.2

        extension UICollectionView {

        func scrollToNextItem() {
            let scrollOffset = CGFloat(floor(self.contentOffset.x + self.bounds.size.width))
            self.scrollToFrame(scrollOffset: scrollOffset)
        }

        func scrollToPreviousItem() {
            let scrollOffset = CGFloat(floor(self.contentOffset.x - self.bounds.size.width))
            self.scrollToFrame(scrollOffset: scrollOffset)
        }

        func scrollToFrame(scrollOffset : CGFloat) {
            guard scrollOffset <= self.contentSize.width - self.bounds.size.width else { return }
            guard scrollOffset >= 0 else { return }
            self.setContentOffset(CGPoint(x: scrollOffset, y: self.contentOffset.y), animated: true)
        }
    }

And the usage will be like用法就像

yourCollectionView.scrollToNextItem()
yourCollectionView.scrollToPreviousItem()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM