Recently, I came across a strange need. The page looks like this. In the upper right corner of the page, there is a list of online people, similar to many live studios. When someone comes in, the list has + 1 avatar, and when someone leaves, the list has -1 avatar. And when we do that, it looks like this, the head is to the left of the collectionView, and when there’s a second one, we add one head from left to right. And so on. And I thought, ok, collectionView is always left to right.

But the product little sister, dead or alive do not agree, said I want to change the following as.

This is what happens when someone joins in

When more people join, but also automatically slide to the far left

It was weird, but it didn’t bother us, so the first solution I came up with was I rotated the whole CollectionView 180 degrees. Then rotate the cell 180 degrees. Then insert the data source in reverse order, and finally scroll to the bottom when judging the number of data sources > can be displayed.

Once you’re clear, your code is simple.

Rotate the CollectionView section

self.audienceCollectionView.transform = CGAffineTransformMakeRotation(M_PI);

Rotate cell section

self.contentView.transform = CGAffineTransformMakeRotation(M_PI);

It is very simple to reverse the data source code

Finally, scroll the cell to the far left

Because we’re rotating 180 degrees, so scrolling to the far left is essentially still going to the far right. In addition, when writing scroll, be careful to determine the data source length, do not overstep the bounds.