iOS Development UI Chapter-infinite Carousel (recycle)

Source: Internet
Author: User
Tags uikit
<span id="Label3"></p>iOS Development UI Chapter-infinite Carousel (recycle)<p><p><strong>Reprint: http://www.cnblogs.com/wendingding/p/3888642.html</strong></p></p><p><p></p></p><p><p><strong>I. Infinite Carousel</strong></p></p><p><p><strong>1. Brief description</strong></p></p>In development, it is often necessary to automate the carousel of ads or some images, which is called Infinite scrolling.  In development, it is common practice to use a uiscrollview, add multiple ImageView on the uiscrollview, and then set the Imageview's picture, and the ScrollView scroll range. Previous Practice:<span class="Apple-tab-span"><span class="Apple-tab-span">generally speaking, the number of advertisements or pictures is not too much (photos). therefore, there is not much to consider performance Issues. But if there are too many pictures (such as 16 images, you need to create 16 imageview), then you have to consider performance Issues. </span></span><span class="Apple-tab-span"><span class="Apple-tab-span">What's more, If you dive into a small picture-browsing program, you might be dealing with hundreds or thousands of images, which can cause great memory waste and poor performance. </span></span><span class="Apple-tab-span">a <span class="Apple-tab-span">large number of pictures:</span></span>When the user is viewing the first picture, the next 7 are created too early, and the user may not be able to see it at all (no interest in looking at the back after reading the previous few). Optimize the Idea: only when need to use, re-create, create the ImageView for village Recycling.  Good practice, no matter how many pictures, only need to create 3 ImageView is Enough. This article describes the use of CollectionView to achieve infinite scrolling recycling. It supports scrolling in both vertical and horizontal directions.<strong><strong>second, the realization</strong></strong>1. description: Collectioncell usage and Tableviewcell usage is not the same, Collectioncell need to register, tell it this identity corresponds to the cell is what type of cell, if not in the cache pool, Then it detects what type of cell this identity is registered for and automatically creates this type of Cell.      2. Implementation Step (1) Add a Uicollectionview to storyboard to adjust the width height of the Control. (2) set its width height = = The width of a picture = = the width of its one cell sets the size of the Cell's lattice.           It defaults to scrolling upward, adjusting to horizontal scrolling. (3) connect, set its data source and proxy implementation code:<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 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" @interface yyviewcontroller () <uicollectionviewdatasource, uicollectionviewdelegate>12 @property (weak, nonatomic) iboutlet uicollectionview *collectinview;13 @end15 16 @ Implementation YYViewController17-(void) VIEWDIDLOAD19 {[super viewdidload];21//register cell22 static Nsstri ng *[email protected] "cell"; [self.collectinview Registerclass:[uicollectionviewcell class] Forcellwithreuseidentifier:id];24}26 #pragma mark-uicollectionviewdatasource28//total number of groups, The default is 1 Groups-(nsinteger) num Berofsectionsincollectionview: (uicollectionview *) collectionView30 {return 1;32}33-(nsinteger) collectionview: ( Uicollectionview *) collectionview numberofitemsinsection: (nsinteger) section34 {return 16;36}37-(uicollectionvi Ewcell *) collectionview: (uicollectionview *) CollectionView cellforitematindexpath: (nsindexpath *) indexPath39 {+-static nsstring *[email protected] "cell"; Uicollectionviewcell *cell=[collectionview Dequeuereusablecellwithreuseidentifier:id forIndexPath:indexPath];42 cell.backgroundcolor=yyrandomcolor;43 return cell;44}45 #pragma mark-uicollectionviewdelegate47 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Interface Display: Print to see if the cell recycling is Implemented.  As you can see, only two cells were created in the entire Program.    (4) Display pictures, Custom cell (two ways, You can use Xib can also use code).        Customize a cell to display Images. Implementation Code:<br>YYimageCell.h file<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// YYimageCell.h 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 <uikit/uikit.h>10 @interface yyimagecell:uicollectionviewcell12 @property (nonatomic,copy ) NSString *icon;13 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>YYIMAGECELL.M file</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyimagecell.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 "YYimageCell.h" @interface yyimagecell () @property (nonatomic,strong) uiimageview *imageview; @end14 @implementation YYimageCell15-(id) initwithframe: (cgrect) frame17 {self = [super initwithframe:frame ];19 if (self) { uiimageview *imageview=[[uiimageview alloc]init];22 [self addsubview: imageview];23 self.imageview=imageview;24 }25 return self;26}27-(void) setIcon: (nsstring *) Icon29 { _icon=[icon copy];31 self.imageview.image=[uiimage imagenamed:icon];32}33-(void) LAYOUTSUBVIEWS35 { layoutsubviews];37 [super self.imageview.frame=self.bounds;38}39 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>YYVIEWCONTROLLER.M file<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 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 "YYimageCell.h" #define Yycell @ "cell" @interface Yyviewcontrol Ler () <uicollectionviewdatasource,uicollectionviewdelegate>15 @property (weak, nonatomic) iboutlet Uicollectionview *collectinview;16 @end18 @implementation YYViewController20-(void) viewDidLoad22 {[super Viewdidload];24//register cell25//static nsstring *[email protected] "cell"; [self.collectinview REGISTERCLA Ss:[yyimagecell class] forcellwithreuseidentifier:yycell];27}29 #pragma mark-uicollectionviewdatasource31//altogether Number of groups, default to 1 group-(nsinteger) numberofsectionsincollectionview: (uicollectionview *) collectionView33 {return 1;35}36-(N Sinteger) collectionview: (uicollectionview *) collectionview numberofitemsinsection: (nsinteger) section37 {return 16;39}40-(uicollEctionviewcell *) collectionview: (uicollectionview *) collectionview cellforitematindexpath: (NSIndexPath *) IndexPath42 {//static nsstring *[email protected] "cell"; Yyimagecell *cell=[collectionview Dequeuereusab Lecellwithreuseidentifier:yycell forindexpath:indexpath];45 cell.backgroundcolor=yyrandomcolor;46 NSLog (@ "%p,%d", Cell,indexpath.item) cell.icon=[nsstring stringwithformat:@ "minion_%02d", indexpath.item+1];48 return cell;49}5 0 Wuyi #pragma mark-uicollectionviewdelegate52 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Interface implementation: (5) Detail processing Set paging adjust spacing to hide the horizontal scroll bar. Clears its Color.<p><p>iOS Development UI Chapter-infinite Carousel (recycle)</p></p></span>

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.