Implementation steps of the Carousel diagram

Source: Internet
Author: User

1>.h declaring properties
@interface**pagecontrol;
2>.M implementation Steps
@implementationRootview-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {//Add a child view[self addallviews]; }    returnSelf ;}//Add a child view- (void) addallviews{//Layout ScrollView//1. Create an ObjectSelf.scrollview =[[Uiscrollview alloc] initWithFrame:self.frame]; //2. Defining Attributes//Apple 4s allocated memory: 30W 5s:80w 6s:100; //Set Scrolling rangeSelf.scrollView.contentSize = Cgsizemake (Cgrectgetwidth (self.frame) *7,0); //put 7 pictures on the ScrollView     for(inti =0; I <7; i + +) {Uiimageview*imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (cgrectgetwidth (self.frame) * I,0, Cgrectgetwidth (Self.frame), Cgrectgetheight (Self.frame))]; NSString*name = [NSString stringWithFormat:@"%d.png", i +Ten]; Imageview.image=[UIImage Imagenamed:name]; //add ImageView to ScrollView[Self.scrollview Addsubview:imageview]; }    //set the entire page slideself.scrollView.pagingEnabled =YES; //3. Add to Parent View[self addSubview:self.scrollView]; //Layout PagecontrolSelf.pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, Cgrectgetheight (Self.frame)- +, Cgrectgetwidth (Self.frame), +)]; Self.pageControl.backgroundColor=[Uicolor Graycolor]; //number of small dotsSelf.pageControl.numberOfPages =7; //number of current small dotsSelf.pageControl.currentPageIndicatorTintColor =[Uicolor Redcolor]; //no small dots to be selectedSelf.pageControl.pageIndicatorTintColor =[Uicolor Lightgraycolor];    [Self AddSubview:self.pageControl]; }
3> set in ViewcontrollerUiscrollviewThe agent delegate and givesUipagecontrolAdd Event
@interfaceRootviewcontroller () <UIScrollViewDelegate>@property (nonatomic, strong) Rootview*Rootview;@end@implementationRootviewcontroller- (void) loadview{Self.rootview=[[Rootview alloc] Initwithframe:[uiscreen mainscreen].bounds]; Self.view=Self.rootview;}- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view. //4. Setting up the agentSelf.rootView.scrollView.Delegate=Self ; //add an event to Pagecontrol[Self.rootView.pageControl addtarget:self Action: @selector (Pagecontrolclick:) forControlEvents:        Uicontroleventvaluechanged]; //Start adding timers for automatic scrolling[Nstimer Scheduledtimerwithtimeinterval:2target:self selector: @selector (timeaction:) Userinfo:nil Repeats:yes]; }//Implement automatic scrolling- (void) Timeaction: (Nstimer *) time{NSLog (@"timed"); //get current Pagecontrol (pages)Nsinteger index =Self.rootView.pageControl.currentPage; //if it's not the last page, offset one page backwards    if(Index! = self.rootView.pageControl.numberOfPages-1) {Index++; } Else if(Index = = self.rootView.pageControl.numberOfPages-1){        //If this is the last page, skip to the first pageindex =0; }     //to modify the offset of a scrollview by index//Self.rootView.scrollView.contentOffset = cgpointmake (Index * cgrectgetwidth (Self.view.frame), 0);[Self.rootView.scrollView setcontentoffset:cgpointmake (Index * cgrectgetwidth (self.view.frame),0) Animated:yes]; Self.rootView.pageControl.currentPage=index;}//Implement the Click Pagecontrol method- (void) Pagecontrolclick: (Uipagecontrol *) pagecontrol{//Small dot positionNsinteger Currentindex =Self.rootView.pageControl.currentPage; //Self.rootView.scrollView.contentOffset = Cgpointmake (currentindex * self.view.frame.size.width, 0);[UIView animatewithduration:0.5animations:^{Self.rootView.scrollView.contentOffset= Cgpointmake (Currentindex * self.view.frame.size.width,0); }];}//Implementing Proxy Methods- (void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{//change the current display position of the Pagecotrol//get the current display locationCGFloat CurrentX =Self.rootview.scrollview.contentoffset.x; Self.rootView.pageControl.currentPage= CurrentX/Self.view.frame.size.width; }

Implementation steps of the Carousel diagram

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.