1 #import "ViewController.h"2 3 #defineWidth Self.view.frame.size.width4 #defineHeight Self.view.frame.size.height5 #defineVIEWH 2006 @interfaceViewcontroller () <UIScrollViewDelegate>7 8 9@property (nonatomic,retain) Uiscrollview *ScrollView;Ten@property (nonatomic,retain) Uipagecontrol *Pagecontrol; One A @end - - @implementationViewcontroller the - - - #pragmaMark-view lifecycle + - +- (void) Viewdidload A { at - [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. - -Self.scrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (0, height-Viewh, Width, VIEWH)]; -Self.scrollview.Delegate=Self ; in[Self.scrollview Setcontentsize:cgsizemake (width*4, VIEWH)]; - //self.scrollView.showsHorizontalScrollIndicator = YES; toSelf.scrollView.showsVerticalScrollIndicator =YES; +self.scrollView.pagingEnabled =YES; - [Self.scrollview setbackgroundcolor:[uicolor redcolor]; the *Nsarray*arr =[[Nsarray alloc]initwithobjects:[uicolor Graycolor],[uicolor Greencolor],[uicolor blueColor],[UIColor Yellowcolor], nil]; $ for(intI=0; i<4; i++) {Panax NotoginsengUIView * View1 = [[UIView alloc]initwithframe:cgrectmake (Width*i,0, Width, VIEWH)]; - [View1 Setbackgroundcolor:arr[i]]; the [Self.scrollview Addsubview:view1]; + A } the [Self.view AddSubview:self.scrollView]; + - $Self.pagecontrol = [[Uipagecontrol alloc]initwithframe:cgrectmake (0,0, $, -)]; $Self.pageControl.center = Cgpointmake (width/2, height- -); - -Self.pageControl.numberOfPages =4; the [Self.pagecontrol addtarget:self Action: @selector (pagechanged:) forcontrolevents:uicontroleventvaluechanged]; - Wuyi [Self.view AddSubview:self.pageControl]; the } --(void) Pagechanged: (uipagecontrol*) Page Wu { - intp = (int) Page.currentpage; About[Self.scrollview Scrollrecttovisible:cgrectmake (Width*p,0, Width, VIEWH) Animated:yes]; $ } - --(void) Scrollviewdidscroll: (uiscrollview*) ScrollView - { ACGFloat Pagewith =ScrollView.frame.size.width; + intpage = Floor ((scrollview.contentoffset.x-pagewith/2)/pagewith) +1; theSelf.pageControl.currentPage =page; - } $ the the the the -- (void) didreceivememorywarning { in [Super didreceivememorywarning]; the //Dispose of any resources the can be recreated. the } About the @end
IOS Learning notes, ScrollView add pagination