iOS Development UI-unlimited carousel (fully functional)

Source: Internet
Author: User

iOS Development UI-unlimited 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:

  1//2//YYVIEWCONTROLLER.M 3//07-Unlimited Scrolling (recycling) 4//5//Created by Apple on 14-8-3. 6//Copyright (c) 2014 Yangyong.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "MJExtension.h" #import "YYnews.h" #import "YYcell.h" #de Fine Yyidcell @ "Cell" 15//NOTE: Here you need to consider the user's manual drag-and-drop #define Yymaxsections @interface Yyviewcontroller () <uicollection Viewdatasource,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;     (void) Viewdidload 38 {39[Super Viewdidload]; 40//Registered cell//[Self.collectinview Registerclass:[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 INdexpathsforvisibleitems]lastobject]; 70 71//immediately display the data back to the middle of the group Nsindexpath *currentindexpathrest=[nsindexpath IndexPathForItem:currentIndexPath.ite M 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, default to 1 group-(Nsinteger) numBerofsectionsincollectionview: (Uicollectionview *) CollectionView 94 {yymaxsections return; Lectionview: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section 98 {return Self.news.cou nt;100}101 102-(Uicollectionviewcell *) 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 re Turn cell;108}109 #pragma mark-uicollectionviewdelegate111//When the user starts to drag and drop the call to the (void) scrollviewwillbegindragging :(Uiscrollview *) scrollView113 {[Self removenstimer];115}116///When the user stops dragging 117-(void) Scrollviewdidenddragging: ( Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate118 {119 [Self addnstimer];120}121//Set page number 122-(void) Scrollvi Ewdidscroll: (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)

iOS Development UI-unlimited carousel (fully functional)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.