Infinite Carousel (Fully functional)
First, automatic scrolling
Add and set a timer, each 2 seconds, jumps to the next bar.
Gets the location that is currently being displayed.
1 [self addnstimer]; 2} 3 4-(void) Addnstimer 5 {6// Nstimer timerwithtimeinterval:<# (nstimeinterval) # > target:<# (ID) #> selector:<# (SEL) #> userinfo:<# (ID) #> repeats:<# (BOOL) #> 7 [Nstimer scheduledtimerwithtimeinterval:1.5 target:self selector: @selector (nextPage) Userinfo:nil Repeats:yes]; 8} 9-(void) NextPage10 { NSLog (@ "%@", [Self.collectinview Indexpathsforvisibleitems]);// Nsindexpath * Currentindexpath=[[self.collectinview indexpathsforvisibleitems]lastobject];13}
Print View:
Implementation steps:
(1) Adding and setting timers
(2) Setting the call method of the timer
1) Get the location you are currently showing
2) Calculate the next location to show
3) Scroll to the next position by animation
Note: Judgment is required.
Implementation code:
1-(void) Viewdidload 2 {3 [Super Viewdidload]; 4//Registered cell 5//[Self.collectinview Registerclass:[yyimagecel L class] Forcellwithreuseidentifier:yycell]; 6 [Self.collectinview registernib:[uinib nibwithnibname:@ "Yycell" Bundle:nil] forcellwithreuseidentifier:yyidcell]; 7 8 9 [Self.collectinview scrolltoitematindexpath:[nsindexpath indexpathforitem:0 insection:50] AtScrollPos Ition:uicollectionviewscrollpositionleft animated:yes];10 [Self addnstimer];11}12-(void) AddNSTimer14 {//N Stimer timerwithtimeinterval:<# (nstimeinterval) #> target:<# (ID) #> selector:<# (SEL) #> UserInfo: <# (ID) #> repeats:<# (BOOL) #>16 nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.5 target:se LF selector: @selector (nextPage) Userinfo:nil repeats:yes];18//Add to Runloop [[Nsrunloop Mainrunloop]addtimer:time R formode:nsrunloopcommonmodes];20}21-(void) NextPage22 {//NSLog (@ "%@", [Self.collectinview IndexpathsforvIsibleitems]); 24//1) Gets the position currently being displayed. Nsindexpath *currentindexpath=[[self.collectinview Indexpathsforvisibleitem S]lastobject];26 27//2) Calculate the next location to show Nsinteger nextitem=currentindexpath.item+1;29 Nsinteger nextsec tion=currentindexpath.section;30 if (nextitem==self.news.count) {nextitem=0;32 nextsection++;33 }34 Nsindexpath *nextindexpath=[nsindexpath indexpathforitem:nextitem insection:nextsection];35 36//3) by animation Scroll to the next location PNS [Self.collectinview scrolltoitematindexpath:nextindexpath atscrollposition: Uicollectionviewscrollpositionleft animated:yes];38}
Description of the timer:
When the user is dealing with something else, the timer stops working. The timer should be added to the Runloop to tell the system to give it a portion of the space when dealing with other things.
Second, set the page number
Add a page number control in storyboard.
Implementation code:
1 #pragma mark-Lazy load 2-(Nsarray *) News 3 {4 if (_news==nil) {5 _news=[yynews objectarraywithfilename:@ "Newse S.plist "]; 6 self.pagecontrol.numberofpages=self.news.count; 7} 8 return _news; 9}10-(void) viewDidLoad12 {[Super VIEWDIDLOAD];14//Registered CELL15//[Self.collectinview Registerclass:[yyim Agecell class] forcellwithreuseidentifier:yycell];16 [Self.collectinview registernib:[uinib nibWithNibName:@ "YYCell [Bundle:nil] forcellwithreuseidentifier:yyidcell];17 [Self.collectinview scrolltoitematindexpath:[nsin Dexpath indexpathforitem:0 insection:50] Atscrollposition:uicollectionviewscrollpositionleft animated:YES];20 [self Addnstimer];21}22-(void) AddNSTimer24 {//Nstimer timerwithtimeinterval:<# (nstimeinterval) #> target:<# (ID) #> selector:<# (SEL) #> userinfo:<# (ID) #> repeats:<# (BOOL) #>26 nstimer *timer = [NSTimer scheduledtimerwithtimeinterval:1.5 target:self Selector: @selector (nextPage) Userinfo:nil repeats:yes];28//Added to Runloop [[Nsrunloop Mainrunloop]addtimer:timer Formode:n Srunloopcommonmodes];30}31-(void) NextPage32 {//NSLog (@ "%@", [Self.collectinview Indexpathsforvisibleitems]); 34 1) Get the location currently being displayed. Nsindexpath *currentindexpath=[[self.collectinview indexpathsforvisibleitems]lastobject];36 37//2) Calculate the next location to show Nsinteger nextitem=currentindexpath.item+1;39 Nsinteger Nextsection=currentindexpat h.section;40 if (nextitem==self.news.count) {nextitem=0;42 nextsection++;43}44 Nsindexpath *nextindexpath=[nsindexpath Indexpathforitem:nextitem insection:nextsection];45 46//3) Scroll to next position by animation [sel F.collectinview Scrolltoitematindexpath:nextindexpath Atscrollposition:uicollectionviewscrollpositionleft ANIMATED:YES];48 49//4) set page number self.pagecontrol.currentpage=nextitem;51}
Automatic scrolling, page number implementation effect:
Third, improve
Note: Listen to the CollectionView scrolling, when manually touch the CollectionView, try to drag the time, the timer stop, when the finger moved, restart the timer.
The complete implementation code:
8 9 #import "YYViewController.h" #import "MJExtension.h" #import "YYnews.h" #import "YYcell.h" #define Yyidcell @ "Cell" 15//NOTE: Here you need to consider the user's manual drag-and-drop #define Yymaxsections @interface Yyviewcontroller () <uicollectionview Datasource,uicollectionviewdelegate> @property (Weak, nonatomic) Iboutlet Uicollectionview *collectinview; @property (Nonatomic,strong) Nsarray *news; @property (Weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol; @property (Nonatomic,strong) Nstimer *timer; @end @implementation yyviewcontroller #pragma mark-lazy load-(Nsarray *) News {if (_news==nil ) {_news=[yynews objectarraywithfilename:@ "newses.plist"]; self.pagecontrol.numberofpages=self.news . Count; _news return; Viewdidload (void): 40//Registered cell///[Self.collectinview Registerclas.] S:[yyimagecell class] Forcellwithreuseidentifier:yycell]; [Self.collectinView registernib:[uinib nibwithnibname:@ "Yycell" Bundle:nil] forcellwithreuseidentifier:yyidcell]; [Self.collectinview scrolltoitematindexpath:[nsindexpath indexpathforitem:0 insection:50] AtScroll Position:uicollectionviewscrollpositionleft Animated:yes]; [Self addnstimer]; 47} 48 49//Add timer (void) Addnstimer {nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:0.5 target:se LF selector: @selector (nextPage) Userinfo:nil Repeats:yes]; 53//Added to Runloop [[Nsrunloop Mainrunloop]addtimer:timer formode:nsrunloopcommonmodes]; Self.timer=timer; 56} 57 58//Delete timer-(void) Removenstimer [Self.timer invalidate]; self.timer=nil; 63} 64 65//Auto-scrolling -(void) NextPage 67 {68//1 gets the position currently being displayed Nsindexpath *currentindexpath=[[self.collectinview indexpathsfor Visibleitems]lastobject]; 70 71//immediately display the data back to the middle of the group Nsindexpath *currentindexpathrest=[nsindexpath indexpathforitem:currentindexpath.iTEM INSECTION:YYMAXSECTIONS/2]; [Self.collectinview scrolltoitematindexpath:currentindexpathrest atscrollposition: Uicollectionviewscrollpositionleft Animated:no]; 74 75//2 Calculate the next place to show Nsinteger nextitem=currentindexpathrest.item+1; Nsinteger nextsection=currentindexpathrest.section; if (nextitem==self.news.count) {nextitem=0; nextsection++; Bayi} nsindexpath *next Indexpath=[nsindexpath Indexpathforitem:nextitem Insection:nextsection]; 83 84//3 Scroll to next position by animation [Self.collectinview Scrolltoitematindexpath:nextindexpath Atscrollposition:uicoll Ectionviewscrollpositionleft Animated:yes]; 86 87////4) set page number//Self.pagecontrol.currentpage=nextitem; Mark-uicollectionviewdatasource 92//Total number of groups, the default is 1 group Nsinteger-(Numberofsectionsincollectionview): (UI CollectionView *) CollectionView 94 {yymaxsections) (Nsinteger) CollectionView: (Uicollectionview *) c OllEctionview numberofitemsinsection: (nsinteger) section 98 {in return self.news.count;100}101 102-(Uicollectionviewcel L *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath *) indexPath103 {104 YYcell *cell=[collectionview Dequeuereusablecellwithreuseidentifier:yyidcell forindexpath:indexpath];105 cell.news= self.news[indexpath.item];106 NSLog (@ "%p,%d", Cell,indexpath.item) 107 return cell;108}109 #pragma mark-uicoll ectionViewDelegate111//(void) scrollviewwillbegindragging: (Uiscrollview *) scrollView113 {114] When the user starts to drag and drop [ Self removenstimer];115}116//Call 117 when the user stops dragging-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView Willdecelerate: (BOOL) decelerate118 {119 [Self addnstimer];120}121//Set page number 122-(void) Scrollviewdidscroll: ( Uiscrollview *) scrollView123 {124 int page= (int) (scrollview.contentoffset.x/scrollview.frame.size.width+0.5)% self.news.count;125 self.pagecontrol.currentpage=page;126}127 @end
Additional notes:
The ideal way to achieve waterfall flow is to inherit the Uiscrollview, not the use of multiple UITableView implementations (this way the cell can not be recycled, and after the cell's scrolling time is forbidden, the overall tableview need to be blank with scrolling).
It can also be implemented using CollectionView, but using this method requires a custom CollectionView layout (pipelining layout)
Infinite Carousel (Fully functional)