IOS Package Carousel Diagram

Source: Internet
Author: User

Carousel diagram is a common way, often used in a variety of websites, or apps, of course, as the app's launch view is also a good choice.

The leisure package is a, only for the novice to participate in the examination.

1. Create a new project, set up a Carousel diagram class


Create an empty project, create a new class, named Carousel, and inherit from UIView

2. Writing the Carousel class interface
1 @interfaceCarousel:uiview2 typedef ns_enum (nsinteger,uicarouselpagetype) {3     //create an enumerated type to set the style of the carousel4Uicarouselpagetypecenter,//set Pagecontrol in the center5Uicarouselpagetypeleft,//set Pagecontrol on the left6Uicarouselpagetyperight,//set Pagecontrol on the right7 };8@property (Nonatomic,strong) Nsarray *imagearry;//used to receive pictures from outside9@property (nonatomic,assign) nstimeinterval duration;//the duration to receive each pictureTen @property (nonatomic,assign) uicarouselpagetype PageType; One @end
3. Internal code

1. Define Uiscrollview and Uipagecontrol in lazy loading mode

(1) Establishing an extension for the Carousel class

 

1 @interface Carousel () <UIScrollViewDelegate>2 @property (nonatomic,strong) uiscrollview *scroll; 3 @property (nonatomic,strong) Uipagecontrol *Pagecontrol; 4 @property (nonatomic,assign)int  index; 5 @property (nonatomic,strong) Nstimer *timer; 6 @end

(2) Overriding initialization method

1 -(Instancetype) initWithFrame: (cgrect) frame{2Self     =[Super initWithFrame: Frame]; 3     if (self) {4         _timer =[[Nstimer alloc]init]; 5         _index=0; 6     }7     return self ; 8 }

(3) ScrollView

1-(Uiscrollview *) scroll{2     if(_scroll==Nil) {3_scroll =[[Uiscrollview alloc]initwithframe:self.bounds];4_scroll.Delegate=Self ;5_scroll.contentsize=cgsizemake ([_imagearry count]*width,height);6_scroll.pagingenabled=yes;//allow full page flipping7_scroll.bounces=NO;8_scroll.showshorizontalscrollindicator=NO;9_scroll.showsverticalscrollindicator=NO;Ten          for(intI=0; I<[_imagearry Count]; i++) { OneUiimageview *imageview =[[uiimageview alloc]initwithframe:cgrectmake (I*width,0, WIDTH, HEIGHT)]; AUIImage *image=[_imagearry objectatindex:i]; -Imageview.image=image; - [_scroll Addsubview:imageview]; the         } -     } -     return_scroll; -}

(4) Pagecontrol

1-(Uipagecontrol *) pagecontrol{2     if(_pagecontrol = =Nil) {3_pagecontrol =[[Uipagecontrol alloc]init];4 [self setpagecontrolframe];5_pagecontrol.numberofpages=[_imagearry Count];6_pagecontrol.pageindicatortintcolor=[Uicolor Greencolor];7_pagecontrol.currentpageindicatortintcolor=[Uicolor Redcolor];8 [_pagecontrol addtarget:self Action: @selector (change:) forcontrolevents:uicontroleventvaluechanged];9     }Ten     return_pagecontrol; One } A-(void) setpagecontrolframe{ -     //when the pagetype has different values, there are different frame -     if(_pagetype==uicarouselpagetypeleft) { the_pagecontrol.frame=cgrectmake (0, height- -, Max, -); -}Else if(_pagetype = =uicarouselpagetyperight) { -_pagecontrol.frame=cgrectmake (width- Max, height- -, Max, -); -}Else{ +_pagecontrol.frame=cgrectmake (width/2- the, height- -, Max, -); -     } + } A-(void) Change: (Uipagecontrol *) page{ at [_timer invalidate]; -_timer=Nil; -_scroll.contentoffset=cgpointmake (Page.currentpage*width,0); -_index= (int) Page.currentpage;//assign value to index again -_timer =[Nstimer scheduledtimerwithtimeinterval:_duration target:self selector: @selector (Lunbo) Userinfo:nil repeats: YES]; -}

(5) Overriding the set method of Imagearray

1-(void) Setimagearry: (Nsarray *) imagearry{2     //override set method to display ScrollView and Pagecontrol when Imagearray has value3     if(_imagearry!=Imagearry) {4_imagearry =Imagearry;5 [self addSubview:self.scroll];6 [self addSubview:self.pageControl];7_timer =[Nstimer scheduledtimerwithtimeinterval:_duration target:self selector: @selector (Lunbo) Userinfo:nil repeats: YES];8     }9}

(6) Proxy methods for Scroll

1-(void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{2     //destroying the timer when you start dragging3 [_timer invalidate];4_timer =Nil;5 }6-(void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{7     //Regenerate timer at end of deceleration8_index= (int) _pagecontrol.currentpage;//assign value to index again9_timer =[Nstimer scheduledtimerwithtimeinterval:_duration target:self selector: @selector (Lunbo) Userinfo:nil repeats: YES];Ten } One-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{ A_pagecontrol.currentpage=scrollview.contentoffset.x/WIDTH; -}
4. Apply the Carousel class
1 #import "ViewController.h"2 #import "Carousel.h"3 @interfaceViewcontroller ()4 @end5 @implementationViewcontroller6 7- (void) Viewdidload {8 [Super Viewdidload];9Carousel *lunbo =[[carousel Alloc]initwithframe:cgrectmake (0, -,414, -)];TenNsmutablearray *arr=[[nsmutablearray alloc]initwithcapacity:9]; One      for(intindex=1; index<Ten; index++) { AUIImage *image =[uiimage imagenamed:[nsstring stringWithFormat:@"%d.jpg", index]]; - [arr addobject:image]; -     } thelunbo.duration=1; -Lunbo. Pagetype=Uicarouselpagetypecenter; -Lunbo. imagearry=arr; - [Self.view Addsubview:lunbo]; +     //additional setup after loading the view, typically from a nib. -}

IOS Package 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.