iphone - UIPageViewController automatic scrolling in iOS -
iphone - UIPageViewController automatic scrolling in iOS -
i making 1 app, in pageviewcontroller should scroll in time interval. have done till manual scrolling. user can scroll images , work. stuck accomplish automatic scrolling user touches. below code. please help me on this.
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. _pagetitles = @[@"", @"", @"", @""]; _pageimages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"]; // create page view controller self.pageviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"pageviewcontroller"]; self.pageviewcontroller.datasource = self; pagecontentviewcontroller *startingviewcontroller = [self viewcontrolleratindex:0]; nsarray *viewcontrollers = @[startingviewcontroller]; [self.pageviewcontroller setviewcontrollers:viewcontrollers direction:uipageviewcontrollernavigationdirectionforward animated:no completion:nil]; // alter size of page view controller self.pageviewcontroller.view.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50); [self addchildviewcontroller:_pageviewcontroller]; [self.view addsubview:_pageviewcontroller.view]; [self.pageviewcontroller didmovetoparentviewcontroller:self]; } - (pagecontentviewcontroller *)viewcontrolleratindex:(nsuinteger)index { if (([self.pagetitles count] == 0) || (index >= [self.pagetitles count])) { homecoming nil; } // create new view controller , pass suitable data. pagecontentviewcontroller *pagecontentviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"pagecontentviewcontroller"]; pagecontentviewcontroller.imagefile = self.pageimages[index]; pagecontentviewcontroller.titletext = self.pagetitles[index]; pagecontentviewcontroller.pageindex = index; homecoming pagecontentviewcontroller; } -(void)viewwillappear:(bool)animated { [super viewwillappear:yes]; self.navigationcontroller.navigationbar.hidden = yes; } #pragma mark - page view controller info source - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = ((pagecontentviewcontroller*) viewcontroller).pageindex; if ((index == 0) || (index == nsnotfound)) { homecoming nil; } index--; homecoming [self viewcontrolleratindex:index]; } - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = ((pagecontentviewcontroller*) viewcontroller).pageindex; if (index == nsnotfound) { homecoming nil; } index++; if (index == [self.pagetitles count]) { homecoming nil; } homecoming [self viewcontrolleratindex:index]; } - (nsinteger)presentationcountforpageviewcontroller:(uipageviewcontroller *)pageviewcontroller { homecoming [self.pagetitles count]; } - (nsinteger)presentationindexforpageviewcontroller:(uipageviewcontroller *)pageviewcontroller { homecoming 0; }
@daniel link broken , "answer" little explain solution question. interested in reading documentation uipageviewcontroller class.
the method pay attending here :
- (void)setviewcontrollers:(nsarray *)viewcontrollers direction:(uipageviewcontrollernavigationdirection)direction animated:(bool)animated completion:(void (^)(bool finished))completion
the uipageviewcontroller expects tell info deliver , controller utilize based on value. here telling uipagecontroller subclass delegate , datasouce. after configuring delegate methods , viewcontrolleratindex datasource method, added custom interval calls loadnextcontroller method. in method checking see if "next" controller nil. if not nil pass "next" controller @[starting controller] sec phone call setviewcontrollers. if nil reset index value 0 , phone call setviewcontrollers "next" controller @[starting controller]. custom subclass serves page next or first controller automatically. bonus user interaction still handled uipageviewcontroller class.
note: initial phone call setviewcontrollers required because uipageviewcontrller not know if viewcontroller can loaded first. sec phone call setviewcontrollers nil more skip next or first viewcontroller.
- (void)viewdidload { [super viewdidload]; self.index = 0; self.delegate = self; self.datasource = self; /*! setup interval */ [nstimer scheduledtimerwithtimeinterval:10.0 target:self selector:@selector(loadnextcontroller) userinfo:nil repeats:yes]; /*! set starting controller */ nsarray *startingviewcontrollers = @[[self viewcontrolleratindex:self.index]]; [self setviewcontrollers: startingviewcontrollers direction: uipageviewcontrollernavigationdirectionforward animated: yes completion: nil]; } - (uiviewcontroller*)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = ((yspagecontentviewcontroller*) viewcontroller).pageindex; if (index > 0 ) { homecoming [self viewcontrolleratindex:index-1]; } homecoming nil; } - (uiviewcontroller*)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger index = ((yspagecontentviewcontroller*) viewcontroller).pageindex; if (index+1 < [self.items count] ) { homecoming [self viewcontrolleratindex:index+1]; } homecoming nil; } - (pagecontentviewcontroller *)viewcontrolleratindex:(nsuinteger)index{ if (index < [self.items count]) { pagecontentviewcontroller *pagecontentviewcontroller = [[pagecontentviewcontroller alloc]init]; pagecontentviewcontroller.pageindex = index; pagecontentviewcontroller.image = (uiimage*)self.items[index]; homecoming pagecontentviewcontroller; } homecoming nil; } /*! set next controller */ - (void)loadnextcontroller { pagecontentviewcontroller *nextviewcontroller = [self viewcontrolleratindex:self.index++]; if (nextviewcontroller == nil) { self.index = 0; nextviewcontroller = [self viewcontrolleratindex:self.index]; } [self setviewcontrollers:@[nextviewcontroller] direction:uipageviewcontrollernavigationdirectionforward animated:yes completion:nil]; }
note: work; not returning right controller when internal gesture recognizer changes controller. somewhere along way losing track of index. update finished implementation.
ios iphone sdk uipageviewcontroller
Comments
Post a Comment