IOS uses Uicollectionview to achieve infinite carousel effect _ios

Source: Internet
Author: User
Tags instance method time interval

First, the case demonstration

This case demo demo is a home page carousel case, support manual carousel and automatic Carousel. Knowledge points are mainly concentrated in the use of Uicollectionview and Nstimer.

Ii. Knowledge Reserves

2.1, Uicollectionview Horizontal layout

Only need to set the Uicollectionviewflowlayout scrolldirection for Uicollectionviewscrolldirectionhorizontal.

2.2, the basic use of Nstimer

Initialization of Nstimer:

Copy Code code as follows:
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(nullable ID) UserInfo repeats: (BOOL) Yesorno;

1, (Nstimeinterval) TI: Book a timer, set a time interval.
Represents the input of a time interval object, in seconds, a >0 floating-point type value, and if the value <0, the system defaults to 0.1.
2), Target: (ID) Atarget: Represents the Sent object, such as self
3), selector: (SEL) Aselector: Method selector, in time interval, select Invoke an instance method
4), UserInfo: (Nullable ID) UserInfo: Need to pass the reference, can be nil
5), repeats: (BOOL) Yesorno: When Yes, the timer will continue to cycle until the failure or release, when no, the timer will be circulated once the failure.
Open Timer:

Copy Code code as follows:
[[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes];

Turn off timer:

[Self.timer invalidate];

2.3. Automatic carousel and manual carousel switching

When initializing, we turn on the timer by default and perform the function of switching to the next picture on a regular basis. When the user touches the view, we have to turn off the timer and manually switch the Uicollectionview. When the user's hand leaves view, we have to reopen the timer, for automatic carousel switching.

Third, key code analysis

3.1, generate Uicollectionviewflowlayout object, set his scrolling direction for horizontal scrolling

 Uicollectionviewflowlayout *flowlayout = [[Uicollectionviewflowlayout alloc] init];
 Flowlayout.itemsize = Cgsizemake (screen_width);
 Flowlayout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
 flowlayout.minimumlinespacing = 0;

3.2. Initialize the Uicollectionview object

 Uicollectionview *collectionview = [[Uicollectionview alloc] Initwithframe:cgrectmake (0, Self.navbarheight, SCREEN_ WIDTH,) collectionviewlayout:flowlayout];
 Collectionview.delegate = self;
 Collectionview.datasource = self;
 Collectionview.showshorizontalscrollindicator = NO;
 collectionview.pagingenabled = YES;
 Collectionview.backgroundcolor = [Uicolor clearcolor];
 [Self.view Addsubview:collectionview];

3.3, Uicollectionview Uicollectionviewdatasource proxy method

#pragma mark-uicollectionviewdatasource
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return
 yymaxsections;
}

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section{
 return self.newses.count;
}

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{


 Yycell *cell = [CollectionView Dequeuereusablecellwithreuseidentifier:yyidcell forIndexPath: Indexpath];
 if (!cell) {
 cell = [[Yycell alloc] init];
 }
 Cell.news=self.newses[indexpath.item];
 return cell;
}

3.4, the timer to open and close

#pragma mark Add timer
-(void) addtimer{
 nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self Selector: @selector (nextpage) Userinfo:nil Repeats:yes];
 [[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
 Self.timer = timer;

}

#pragma mark Delete Timer
-(void) removetimer{
 [Self.timer invalidate];
 Self.timer = nil;
}

3.5. Manual switching and automatic carousel switching

-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{
 [self removetimer];
}

#pragma mark when the user stops calling
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{
 [self addtimer];

}

#pragma mark set page number
-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{
 int page = (int) ( scrollview.contentoffset.x/scrollview.frame.size.width+0.5)%self.newses.count;
 Self.pageControl.currentPage =page;
}

3.6, automatic carousel switch to the next view method

-(void) nextpage{
 nsindexpath *currentindexpath = [[Self.collectionview indexpathsforvisibleitems] lastObject];

 Nsindexpath *currentindexpathreset = [Nsindexpath indexPathForItem:currentIndexPath.item INSECTION:YYMAXSECTIONS/2] ;
 [Self.collectionview scrolltoitematindexpath:currentindexpathreset atscrollposition: Uicollectionviewscrollpositionleft Animated:no];

 Nsinteger Nextitem = Currentindexpathreset.item +1;
 Nsinteger nextsection = currentindexpathreset.section;
 if (nextitem==self.newses.count) {
 nextitem=0;
 nextsection++;
 }
 Nsindexpath *nextindexpath = [Nsindexpath indexpathforitem:nextitem insection:nextsection];

 [Self.collectionview Scrolltoitematindexpath:nextindexpath Atscrollposition:uicollectionviewscrollpositionleft Animated:yes];
}

Demo Download Address: Https://github.com/yixiangboy/YXCollectionView

The above is the entire content of this article, I hope to help you learn.

Related Article

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.