iOS development-Uipagecontrol implements paging image carousel

Source: Internet
Author: User

Paging related properties
As long as theUiscrollviewThe Pageenabled property is set toYES,Uiscrollviewwill be divided into a number of separate pages, the contents of the content will be able to carry out pagination will generally be combined with uipagecontrol enhance the paging effect, Uipagecontrol commonly used properties such as a total of how many pages@property(nonatomic)Nsintegernumberofpages; The page number that is currently displayed@property(nonatomic)NsintegerCurrentPage; Whether you need to hide the page number indicator when there is only one page@property(nonatomic)BOOLHidesforsinglepage; Color of other page number indicators@property(nonatomic, retain)Uicolor*pageindicatortintcolor; The color of the current page number indicator@property(nonatomic, retain)Uicolor*currentpageindicatortintcolor;
Paging Picture Carousel Instance
#define Imagecount5#Import "ViewController.h"@interfaceViewcontroller () <UIScrollViewDelegate>@property(Weak, nonatomic) Iboutlet Uiscrollview *scrollview;@property(Weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol;/** * Timer * *@property(Nonatomic,strong) nstimer* timer;@end@implementationviewcontroller-(void) Viewdidload {[SuperViewdidload];//0. Some fixed size parametersCGFloat imagew=self.scrollview.frame.size.width;    CGFloat imageh=self.scrollview.frame.size.height; CGFloat imagey=0;//1. Adding imagecount pictures to ScrollView     for(intI=0; i<imagecount; i++) {uiimageview* imageView =[[uiimageview alloc]init];//Set frameCGFloat Imagex=i*imagew; Imageview.frame=cgrectmake (ImageX, Imagey, Imagew, Imageh);//Set Picturensstring* name=[nsstring stringwithformat:@"img_0%d", i+1];        Imageview.image=[uiimage Imagenamed:name];    [Self.scrollview Addsubview:imageview]; }//2. Setting the content sizeCGFloat Contentw=imagecount*imagew; Self.scrollview.contentsize=cgsizemake (CONTENTW,0);//3. Hiding the horizontal scroll barSelf.scrollview.showshorizontalscrollindicator=no;//4. PagingSelf.scrollview.pagingenabled=yes; self.scrollview.delegate=self;//5. Set the total number of pages for PagecontrolSelf.pagecontrol.numberofpages=imagecount;//6. Adding a timer (call the self's NextImage method every 2 seconds)[Self addtimer];}/** * Add Timer * *- (void) addtimer{Self.timer=[nstimer Scheduledtimerwithtimeinterval:2.0Target:self selector:@selector(nextimage) Userinfo:nil Repeats:yes];//Cyclic control multithreading[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];}/** * Remove timer * /- (void) removetimer{[Self.timer invalidate]; Self.timer=nil;}/** * Next photo * /- (void) nextimage{//1. Increase the page number of Pagecontrol    intPage=0;if(self.pagecontrol.currentpage==imagecount-1) {page=0; }Else{page=self.pagecontrol.currentpage+1; }//2. Calculating the position of ScrollView scrollingCGFloat offsetx=page*self.scrollview.frame.size.width; Cgpoint Offset=cgpointmake (OffsetX,0); [Self.scrollview Setcontentoffset:offset animated:yes];} #pragma mark-Proxy method/** * When ScrollView is scrolling, it will be called */- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//According to ScrollView's scrolling position, Pagecontrol displays the pageCGFloat scrollw=scrollview.frame.size.width;intPage= (scrollview.contentoffset.x+scrollw*0.5)/SCROLLW; Self.pagecontrol.currentpage=page;}/** * Call * * When starting to drag- (void) Scrollviewwillbegindecelerating: (Uiscrollview *) scrollview{//Stop timer (once the timer is stopped, it cannot be used)[Self removetimer];}/** * Call * * When stopping drag and drop- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{//Turn on timer[Self addtimer];}@end
Storyboard

iOS development-Uipagecontrol implements paging image carousel

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.