The realization principle of IOS picture infinite roaster

  • 2020-05-30 21:04:24
  • OfStack

First, the implementation idea: there are only 2 cell in the whole collectionView. The second cell is always displayed in the middle.

Scrolling: scrolling forward the current cell footer is 0, scrolling backward the current cell footer is 2. Using the current cell footer minus 1, get +1, or -1, to make the image index plus 1 or minus 1, realize image switching.

Declare a global variable index to save the index of the image, which is used to toggle the image displayed in the current cell.

Change the script of cell to 1. Code viewDidLoad before scrolling

After completing the first scroll, the code sets the current cell to the second cell(essentially making the currently displayed cell's foot 1)

The last one has one thread problem: when you continue to scroll, and finally stop, the current image will flash, because it is executed asynchronously and concurrently, and the thread is not safe.

To prepare, create collectionViewController,storyboard, class binding,cell binding,cell reusable identity binding.

Create the cell.h file

// PBCollectionCell.h
//  The infinite scroll 
// Created by  Pei bobo  on 16/3/30.
// Copyright © 2016 years   Pei bobo . All rights reserved.
#import <UIKit/UIKit.h>
@interface PBCollectionCell : UICollectionViewCell
@property(nonatomic,strong)UIImage * img;
•cell.m file 
// PBCollectionCell.m
//  The infinite scroll 
// Created by  Pei bobo  on 16/3/30.
// Copyright © 2016 years   Pei bobo . All rights reserved.
#import "PBCollectionCell.h"
@interface PBCollectionCell ()
// Don't forget to give collectionView the cell Set the picture box on , And to the drag line cell In the classification 
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@implementation PBCollectionCell
// rewrite img the set Way to a cell For the assignment .( When calling cell.img = img Is called back when set ..img The method of )
-(void)setImg:(UIImage *)img{
_img = img;
self.imgView.image = img;

Controller code details

// ViewController.m
//  The infinite scroll 
// Created by  Pei bobo  on 16/3/30.
// Copyright © 2016 years   Pei bobo . All rights reserved.
#import "ViewController.h"
#import "PBCollectionCell.h"
// Defines the number of macro slices 
#define kPictureCount 3
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
*  Index of images 
@property(nonatomic,assign) NSInteger index;
• Declare global variables index To concatenate the index of the images switched during scrolling 
@implementation ViewController
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return kPictureCount;
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * ID = @"cell";
PBCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
// Image index is down only 1 Stand or 1 zhang , namely +1, or -1, In order to switch images 
// In the middle of the cell Mark is my foot 1, Scroll back footer 2 or 0, Add up to next A value of +1 or -1( Index images +1 or -1 To switch the image ), the 
NSInteger next = indexPath.item - 1;
// In order not to let next Crossing the line , You take the remainder  ---------+next In order to switch images 
next = (self.index + kPictureCount + next) % kPictureCount;
NSString * imgName = [NSString stringWithFormat:@"%02ld",next + 1];
UIImage * img = [UIImage imageNamed:imgName];
cell.img = img;
return cell;

The & # 8226; In viewDidLoad, cell, which sets the current display image, is the second cell. When cell scrolls forward with footer -1(cell's indexPath.item is 0), scrolls backward with footer +1(cell's indexPath.item is 2).

The & # 8226; How to splice pictures ? -- through the global variable self. index: • cell: self.index-1: cell: indexPath.item: 0; At this point, however, the index of the image needs to be subtracted by 1

The & # 8226; cell scroll back to the image index self.index +1 and cell indexPath.item is 2; Add 1 to the index of the image

The & # 8226; To sum up, we can get the index of the image to be displayed in the next image by cell's indexPath.item-1 plus self.index

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// Calculates an integer multiple of the offset , Offset the 0 Or is it 2, -1 Is to make self.index Image indexing +1  or  -1 To switch pictures ;
NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width - 1;
self.index = (offset + self.index + kPictureCount) % kPictureCount;
// The essence is change cell The index of the , According to self.index I'm gonna switch the image , Use redundancy to keep the image index from crossing the line 
// Asynchronous main queue execution , In order not to let the continuous rolling stop , The picture flashed .
dispatch_async(dispatch_get_main_queue(), ^{
[self scrollToSecondCell];

The & # 8226; When cell stops scrolling, calculate the image index self.index value and save it in the global variable. In order to properly switch the image to be displayed by cell's indexPath.item's +1 or -1 and the image index self.index when the third data source method, cell, returns cell.

The & # 8226; After rolling stop will be put into the queue asynchronous execution operation, the operation is to display cell foot mark into 1 among them, which is the current image cell into second cell. On the home side column executed asynchronously (not blocking the main thread, the home side columns of the task order, after the completion of the task as the main thread to perform switching cell second cell) not be bug continuous rolling after flashing images. (this sentence can't understand can skip).

// encapsulation sets cell currently displayed as the second cell method.

NSIndexPath * idxPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.collectionView scrollToItemAtIndexPath:idxPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
- (void)viewDidLoad {
[super viewDidLoad];
self.flowLayout.itemSize = self.collectionView.bounds.size;
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.flowLayout.minimumLineSpacing = 0;
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.pagingEnabled = YES;
self.collectionView.bounces = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
[self scrollToSecondCell];

Before cell sliding or sliding after every time use code sets the current display cell for second cell, the data source for a third way is to return to cell method of images in the switch using cell indexPath. item - 1 = 1 or + 1, combined with the picture itself self index value. The index will draw is about to roll out cell is shown in the picture 1 or 1 under the picture.

2. cell after scrolling display images to calculate the current cell index. Through scrollview offset contentoffset. x divided by the width of the scrollview, calculate the index to save the current cell pictures. Then sliding cell, calls a third method, data source will use preserved self. index and add cell indexPath item - 1 to calculate the index to display images.

The above content is this site to introduce you IOS picture unlimited roaster principle, I hope to help you!

Related articles: