iOS Development UI article-infinite Carousel (looping show)

Source: Internet
Author: User

A simple explanation

  One problem with the previous program is that it cannot be looped, because there are only five arrays in the plist file, so the first and last ones are gone, and here are some tips for dealing with this kind of loop presentation problem.

  

Method One: Use a For loop, loop 200 times, create a 200*=1000 model, and the default program starts in the 100th group position, there are 500 models forward, there are 500 backward models, creating a loop to show the illusion.

The code is as follows:

1 //2 //YYVIEWCONTROLLER.M3 //07-Infinite 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"Ten #import "MJExtension.h" One #import "YYnews.h" A #import "YYcell.h" -  - #defineYyidcell @ "Cell" the  - @interfaceYyviewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate> -@property (Weak, nonatomic) Iboutlet Uicollectionview *Collectinview; -@property (nonatomic,strong) Nsmutablearray *News; + @end -  + @implementationYyviewcontroller A  at #pragmamark-Lazy Loading - //-(Nsarray *) News - //{ - //if (_news==nil) { - //_news=[yynews objectarraywithfilename:@ "Newses.plist"]; - //    } in //return _news; - //} to-(Nsmutablearray *) News + { -     if(_news==Nil) { the_news=[Nsmutablearray array]; *          for(intI=0; i< $; i++) { $Nsarray *array=[yynews Objectarraywithfilename:@"newses.plist"];Panax Notoginseng [_news Addobjectsfromarray:array]; -         } the     } +     return_news; A } the  +- (void) Viewdidload - { $ [Super Viewdidload]; $     //Register Cell - //[Self.collectinview Registerclass:[yyimagecell class] Forcellwithreuseidentifier:yycell]; -[Self.collectinview registernib:[uinib Nibwithnibname:@"Yycell"Bundle:nil] Forcellwithreuseidentifier:yyidcell]; the      -     //Default to the left of the No. 500 model in group No. 0Wuyi     [Self.collectinview scrolltoitematindexpath:[nsindexpath indexpathforitem: insection:0 ] Atscrollposition:uicollectionviewscrollpositionleft Animated:yes];  the      - } Wu  - #pragmaMark-uicollectionviewdatasource About //Total number of groups, default to 1 groups $-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView - { -     return 1; - } A-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section + { the     returnSelf.news.count; - } $  the-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath the { theYycell *cell=[CollectionView Dequeuereusablecellwithreuseidentifier:yyidcell Forindexpath:indexpath]; theCell.news=Self.news[indexpath.item]; -NSLog (@"%p,%d", Cell,indexpath.item); in     returncell; the } the  About #pragmaMark-uicollectionviewdelegate the @end

Print to see the index (two cells are still created throughout):

  

Description

Default to the left of the No. 500 model in group No. 0

[Self.collectinview scrolltoitematindexpath:<# (Nsindexpath *) #> atscrollposition:<# ( uicollectionviewscrollposition) #> animated:<# (BOOL) #>]

 

Method Two: Set it to have 100 groups, then there is a total of 100*5=500 model. and sets the default index at 0 for group 50th.

The code is as follows:

1 //2 //YYVIEWCONTROLLER.M3 //07-Infinite 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"Ten #import "MJExtension.h" One #import "YYnews.h" A #import "YYcell.h" -  - #defineYyidcell @ "Cell" the  - @interfaceYyviewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate> -@property (Weak, nonatomic) Iboutlet Uicollectionview *Collectinview; -@property (nonatomic,strong) Nsarray *News; + @end -  + @implementationYyviewcontroller A  at #pragmamark-Lazy Loading --(Nsarray *) News - { -     if(_news==Nil) { -_news=[yynews Objectarraywithfilename:@"newses.plist"]; -     } in     return_news; - } to //-(Nsmutablearray *) News + //{ - //if (_news==nil) { the //_news=[nsmutablearray array]; * //for (int i=0; i<200; i++) { $ //nsarray *array=[yynews objectarraywithfilename:@ "Newses.plist"];Panax Notoginseng //[_news Addobjectsfromarray:array]; - //        } the //    } + //return _news; A //} the  +- (void) Viewdidload - { $ [Super Viewdidload]; $     //Register Cell - //[Self.collectinview Registerclass:[yyimagecell class] Forcellwithreuseidentifier:yycell]; -[Self.collectinview registernib:[uinib Nibwithnibname:@"Yycell"Bundle:nil] Forcellwithreuseidentifier:yyidcell]; the      -     //Default to the left of the No. 500 model in group No. 0Wuyi //[Self.collectinview scrolltoitematindexpath:[nsindexpath indexpathforitem:500 insection:0] AtScrollPosition: Uicollectionviewscrollpositionleft Animated:yes]; the      -      [Self.collectinview scrolltoitematindexpath:[nsindexpath Indexpathforitem:0 insection: ] Atscrollposition:uicollectionviewscrollpositionleft Animated:yes];  Wu      - } About  $ #pragmaMark-uicollectionviewdatasource - //Total number of groups, default to 1 groups --(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView - { A      return ;  + } the-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section - { $     returnSelf.news.count; the } the  the-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath the { -Yycell *cell=[CollectionView Dequeuereusablecellwithreuseidentifier:yyidcell Forindexpath:indexpath]; inCell.news=Self.news[indexpath.item]; theNSLog (@"%p,%d", Cell,indexpath.item); the     returncell; About } the  the #pragmaMark-uicollectionviewdelegate the @end

Note: Both methods above create a large number of useless models, which is not advisable. And in real development, it is recommended that the total number of models not be too large, because the frame in which all controls are computed is traversed inside.

If the number of models is too large, resources are consumed.

Suggestions for improvement: You can monitor the scrolling of your fingers and reset the initial middle position when you stop scrolling.

iOS Development UI article-infinite Carousel (looping show)

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.