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

时间:2022-08-25 07:39:15

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.

如果您提供了一些方法,比如在从右以编程方式切换到单元格之后,将动画放得更大,这也会很有帮助。

3 个解决方案

#1


15  

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. To create a custom animation for this however is more difficult. A quick solution depending on what you need could be this answer.

两者都在UICollectionView上,所以你可以使用任何看起来更方便的东西。但是,要为这个创建一个定制动画则更加困难。根据您的需要,一个快速的解决方案可以是这个答案。

#2


6  

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)
}

#3


5  

Swift 4, iOS 11

斯威夫特4,iOS 11

Based on Mr.Bean's answer, here is an elegant way using UICollectionView extension:

根据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) {
        let frame: CGRect = CGRect(x: contentOffset, y: self.contentOffset.y , width: self.frame.width, height: self.frame.height)
        self.scrollRectToVisible(frame, animated: true)
    }
}

Now you can use it wherever you want:

现在你可以在任何你想要的地方使用它:

collectionView.scrollToNextItem()
collectionView.scrollToPreviousItem()

#1


15  

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. To create a custom animation for this however is more difficult. A quick solution depending on what you need could be this answer.

两者都在UICollectionView上,所以你可以使用任何看起来更方便的东西。但是,要为这个创建一个定制动画则更加困难。根据您的需要,一个快速的解决方案可以是这个答案。

#2


6  

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)
}

#3


5  

Swift 4, iOS 11

斯威夫特4,iOS 11

Based on Mr.Bean's answer, here is an elegant way using UICollectionView extension:

根据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) {
        let frame: CGRect = CGRect(x: contentOffset, y: self.contentOffset.y , width: self.frame.width, height: self.frame.height)
        self.scrollRectToVisible(frame, animated: true)
    }
}

Now you can use it wherever you want:

现在你可以在任何你想要的地方使用它:

collectionView.scrollToNextItem()
collectionView.scrollToPreviousItem()