iphone - How Do I Switch Views With PageControl & UIScrollView? -
i'm noob iphone development (3rd day in xcode) , trying implement pagecontrol , scrollview users can swipe between various pages. i'm using this tutorial , can't figure out how load/switch views nib files opposed changing background color of view. appreciated.
my code
modification of pagecontrolexampleviewcontroller.m renamed newsclass2
// creates color list first time method invoked. returns 1 color object list. + (uicolor *)pagecontrolcolorwithindex:(nsuinteger)index { if (__pagecontrolcolorlist == nil) { __pagecontrolcolorlist = [[nsarray alloc] initwithobjects:[uicolor redcolor], [uicolor greencolor], [uicolor magentacolor], [uicolor bluecolor], [uicolor orangecolor], [uicolor browncolor], [uicolor graycolor], nil]; } // mod index list length ensure access remains in bounds. return [__pagecontrolcolorlist objectatindex:index % [__pagecontrolcolorlist count]]; } //changing views instead of colors, not working + (uiview *)pagecontrolviewwithindex:(nsuinteger)index { if (__pagecontrolviewrlist == nil) { __pagecontrolviewrlist = [[nsarray alloc] initwithobjects:[[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], [[uiview alloc] initwithnibname:@"pageview" bundle:nil], nil]; } // mod index list length ensure access remains in bounds. return [__pagecontrolviewlist objectatindex:index % [__pagecontrolviewlist count]]; } // set label , background color when view has finished loading. - (void)viewdidload { pagenumberlabel.text = [nsstring stringwithformat:@"page %d", pagenumber + 1]; self.view.backgroundcolor = [newsclass2 pagecontrolcolorwithindex:pagenumber]; //setting view not working self.view = [newsclass2 pagecontrolviewwithindex:pagenumber]; }
welcome objective c.
first of need set delegate of scroll view like
//in .h file write @interface viewcontroller : uiviewcontroller<uiscrollviewdelegate> // in viewdidload self.scrollview.delegate = self; then write in delegate method of uiscrollview write...
-(void)scrollviewdidscroll:(uiscrollview *)scrollview { cgfloat pagewidth = scrollview.frame.size.width; int page = floor((scrollview.contentoffset.x - pagewidth / 2) / pagewidth) + 1; self.pagecontrol.currentpage = page; } then make ibaction of valuechange pagecontrol .....
- (ibaction)changepage:(id)sender { int page = self.pagecontrolhelp.currentpage; cgrect frame = self.scrollviewhelp.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; [self.scrollviewhelp scrollrecttovisible:frame animated:yes]; } and have done .......
if have confusion please feel free ask......
Comments
Post a Comment