ios - UI Page View Controller Scroll Won't Scroll Backwards -
i have implemented uipageviewcontroller want able scroll infinitely right (as in user swipes left) , able scroll backwards in other direction way start. don''t want user able scroll left (as in user swipe right) on first page, though have 3 pages loop, how can this?
i have figured out how make infinite scroll 3 view controllers right ( in user scrolls left) when user tries scroll left (as in user swipes right) bugs out , screen goes white?
can improve on code user can infinitely scroll both ways, not one? , if possible prevent user scrolling backwards on first panel? thanks.
pageviewcontroller.h
#import <uikit/uikit.h> @interface pageviewcontroller : uipageviewcontroller <uipageviewcontrollerdelegate, uipageviewcontrollerdatasource> @end
pageviewcontroller.m
#import "pageviewcontroller.h" @interface pageviewcontroller () @end @implementation pageviewcontroller { nsarray *myviewcontrollers; } - (void)viewdidload { [super viewdidload]; self.delegate = self; self.datasource = self; uiviewcontroller *p1 = [self.storyboard instantiateviewcontrollerwithidentifier:@"contentviewcontroller1"]; uiviewcontroller *p2 = [self.storyboard instantiateviewcontrollerwithidentifier:@"contentviewcontroller2"]; uiviewcontroller *p3 = [self.storyboard instantiateviewcontrollerwithidentifier:@"contentviewcontroller3"]; myviewcontrollers = @[p1,p2,p3]; [self setviewcontrollers:@[p1] direction:uipageviewcontrollernavigationdirectionforward animated:no completion:nil]; nslog(@"loaded!"); } -(uiviewcontroller *)viewcontrolleratindex:(nsuinteger)index { return myviewcontrollers[index]; } -(uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger currentindex = [myviewcontrollers indexofobject:viewcontroller]; --currentindex; currentindex = currentindex % (myviewcontrollers.count); return [myviewcontrollers objectatindex:currentindex]; } -(uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { nsuinteger currentindex = [myviewcontrollers indexofobject:viewcontroller]; ++currentindex; currentindex = currentindex % (myviewcontrollers.count); return [myviewcontrollers objectatindex:currentindex]; } -(nsinteger)presentationcountforpageviewcontroller: (uipageviewcontroller *)pageviewcontroller { return myviewcontrollers.count; } -(nsinteger)presentationindexforpageviewcontroller: (uipageviewcontroller *)pageviewcontroller { return 0; } @end
you can use flag know initial view (first view).then use scroll enable , disable method prevent swipe.
-(void)setscrollenabled:(bool)enabled forpageviewcontroller:(uipageviewcontroller*)pageviewcontroller{ for(uiview* view in pageviewcontroller.view.subviews){ if([view iskindofclass:[uiscrollview class]]){ uiscrollview* scrollview=(uiscrollview*)view; [scrollview setscrollenabled:enabled]; return; } }
hope work.