ios--Carousel Diagram Implementation

Source: Internet
Author: User

- (void) viewdidload {[Super viewdidload]; /** * Carousel Chart * * @return Nil*/    //Image ArrayImagearr = @[@"Scroll.png",@"Record.png",@"State2.png"]; //scrollview width and heightScrollWidth = ScreenWidth- -; ScrollHeight= (homeviewheight-5)*0.3; //set the contentsize size of the ScrollView, if the contentsize tall in the high ScrollView will appear up and down drag phenomenon, generally set to equal or 0Scrollview.contentsize = Cgsizemake (scrollwidth* (self.imagearr.count+2), scrollheight); //default starts from first page[ScrollView Setcontentoffset:cgpointmake (0,0)]; [ScrollView Scrollrecttovisible:cgrectmake (ScrollWidth,0, ScrollWidth, scrollheight) Animated:no]; ScrollView.Delegate=Self ; //number of points in the PagecontrolPagecontrol.numberofpages =Self.imageArr.count; //start from the first pagePagecontrol.currentpage =0; //Carousel[Pagecontrol addtarget:self Action: @selector (Turnpage) forcontrolevents:uicontroleventvaluechanged]; //Add a picture     for(inti =0; i < Imagearr.count; i + +) {Uiimageview*imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (Scrollwidth*i+scrollwidth,0, ScrollWidth, scrollheight)]; Imageview.image=[UIImage Imagenamed:imagearr[i]]; Imageview.userinteractionenabled=YES;    [ScrollView Addsubview:imageview]; }    //take the last picture on page No. 0Uiimageview *firstimageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0,0, ScrollWidth, scrollheight)]; Firstimageview.image=[UIImage Imagenamed:[imagearr lastobject];    [ScrollView Addsubview:firstimageview]; //take the first picture on the last pageUiimageview *lastimageview = [[Uiimageview alloc] Initwithframe:cgrectmake (scrollwidth* (imagearr.count+1),0, ScrollWidth, scrollheight)]; Lastimageview.image=[UIImage Imagenamed:[self.imagearr firstobject];        [ScrollView Addsubview:lastimageview]; //Timer Loop Playback[self addtimer]; }#pragmaMark--------Carousel Implementation, Uipagecontrol controls automatic carousel----(void) turnpage {Nsinteger page= Pagecontrol.currentpage;//Gets the current page[ScrollView Scrollrecttovisible:cgrectmake (scrollwidth* (page+1),0, Scrollwidth,scrollheight) Animated:no];//Touch Pagecontroller that point and turn back one page +1}- (void) runtimepage {Nsinteger page= Pagecontrol.currentpage;//Gets the current pagepage++; Page= page > (Imagearr.count-1) ?0: page; Pagecontrol.currentpage=page; [Self turnpage];}- (void) AddTimer {Self.mytimer= [Nstimer scheduledtimerwithtimeinterval:2target:self selector: @selector (runtimepage) Userinfo:nil repeats:yes];}#pragmaMark---Uiscrollview proxy method to ensure manual swipe-(void) Scrollviewwillbegindragging: (Uiscrollview *) ScrollView {//turn off the timer to avoid collision with manual sliding[Self.mytimer invalidate];}- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{//when the manual slide is finished, turn on the timer[self addtimer];}- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {cgfloat pagewidth=ScrollWidth; Nsinteger page= Floor ((scrollview.contentoffset.x-pagewidth/([Imagearr count]+2))/pagewidth) +1; Page--;//default starts from second pagePagecontrol.currentpage =page; }- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {cgfloat pagewidth=ScrollWidth; Nsinteger currentpage= Floor ((scrollview.contentoffset.x-pagewidth/([Imagearr count]+2))/PageWidth) +1; if(currentpage==0) {[ScrollView scrollrecttovisible:cgrectmake (ScrollWidth* [Imagearr Count],0, Scrollwidth,scrollheight) Animated:no];//serial 0 last 1 page    }    Else if(currentpage== ([Imagearr count]+1) {[ScrollView scrollrecttovisible:cgrectmake (ScrollWidth,0, Scrollwidth,scrollheight) Animated:no];//the last +1, the 1th page of the cycle    }    }

ios--Carousel Diagram Implementation

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.