1 #import "RootViewController.h"2 3 #definewidth [UIScreen mainscreen].bounds.size.width4 #defineHeigthy 1505 6 @interfaceRootviewcontroller () <UIScrollViewDelegate>7 {8Uiscrollview *_scrollview;9Nsmutablearray *Imagearray;TenUipagecontrol *Pagecontrol; One } A @end - - @implementationRootviewcontroller the -- (void) Dealloc - { -Imagearray =Nil; + [Super Dealloc]; - } + A- (void) Loadview at { - [Super Loadview]; -_scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0,0, Width, heigthy)]; -_scrollview.pagingenabled =YES; -_scrollview.Delegate=Self ; - //start by selecting the layout of the second picture picture [3-1-2-3-1] in_scrollview.contentoffset = Cgpointmake (width,0); - //Hide horizontal scroll bar to_scrollview.showshorizontalscrollindicator =NO; + -Pagecontrol = [[Uipagecontrol alloc] initwithframe:cgrectmake (Width- -, Heigthy- -, -, -)]; the //setting Pagecontrol does not support user action *pagecontrol.userinteractionenabled =NO; $Pagecontrol.currentpageindicatortintcolor =[Uicolor Redcolor];Panax NotoginsengPagecontrol.pageindicatortintcolor =[Uicolor Greencolor]; - [Self.view Addsubview:_scrollview]; the [Self.view Addsubview:pagecontrol]; + [_scrollview release]; A [Pagecontrol release]; the + } - $- (void) Viewdidload { $ [Super Viewdidload]; -Imagearray =[[Nsmutablearray alloc] init]; -Nsarray *temparray = @[@"1-3.jpg",@"1-1.jpg",@"1-2.jpg",@"1-3.jpg",@"1-1.jpg"]; the [Imagearray Addobjectsfromarray:temparray]; - //set the content size of the _scrollview based on the number of ImagearrayWuyi_scrollview.contentsize = Cgsizemake (Width *Imagearray.count, heigthy); thePagecontrol.numberofpages = Imagearray.count-2; - //add a picture to _scrollview Wu [self addimageswithscrollview]; - } About $ /** - * Add pictures to ScrollView - */ -- (void) Addimageswithscrollview A { + for(inti =0; i < Imagearray.count; i++) { theUiimageview *imageview =[[Uiimageview alloc] initwithimage:[uiimage imagenamed:imagearray[i]]; -Imageview.frame = CGRectMake (i * width,0, width, heigthy); $ [_scrollview Addsubview:imageview]; the [ImageView release]; the } the } the - #pragmaMethods of Mark-uiscrollviewdelegate in- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView the { the intImageIndex = scrollview.contentoffset.x/width; About if(ImageIndex = =0) { the //when you scroll to the first picture, jump to the penultimate picture the[_scrollview Scrollrecttovisible:cgrectmake (Imagearray.count-2) *width,0, Width, heigthy) animated:no]; the}Else if(ImageIndex = = Imagearray.count-1){ + //when you scroll to the last picture, jump to the second picture -[_scrollview scrollrecttovisible:cgrectmake (width,0, Width, heigthy) animated:no]; the }Bayi } the the /** - * Set the current page of Pagecontrol - */ the- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView the { the //Plus 0.5 is for the user experience is better, the sliding process which picture is dominant to show the picture of the corresponding subscript the intImageIndex = Scrollview.contentoffset.x/width +0.5; - if(ImageIndex = =0) { the //set the appropriate subscript (minus 1 to correspond to the subscript of the Pagecontrol) theImageIndex = Imagearray.count-1; the}Else if(ImageIndex = = Imagearray.count-1){94 //set the appropriate subscript (minus 1 to correspond to the subscript of the Pagecontrol) theImageIndex =1; the } thePagecontrol.currentpage = ImageIndex-1;98 } About @end
Uiscrollview Reality Cycle scrolling