I have a UIScrollView which I have added three views Z, A and B as such:

What I would like to accomplish is have the three views be set in a "rotating" manner centered on view A appearing first. When a user swipes left and sees view B, swiping left again gets to view Z, and vise versa, swiping right when at view Z takes the user to view B.
I have setup the code for self.scrollview as such:
ZViewController *zViewController = [[ZViewController alloc] init];
[self addChildViewController:zViewController];
[self.scrollView addSubview:zViewController.view];
[zViewController didMoveToParentViewController:self];
AViewController *aViewController = [[AViewController alloc] init];
CGRect aframe = aViewController.view.frame;
aframe.origin.x = 320;
aViewController.view.frame = aframe;
[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];
BViewController *bViewController = [[BViewController alloc] init];
CGRect bframe = bViewController.view.frame;
bframe.origin.x = 640;
bViewController.view.frame = bframe;
[self addChildViewController:bViewController];
[self.scrollView addSubview:bViewController.view];
[bViewController didMoveToParentViewController:self];
self.scrollView.contentSize = CGSizeMake(960, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;
However, presently I am unsure how to proceed in terms getting the "rotation" element to work (i.e. swiping on B gets to Z) and would appreciate any help.
Thanks!
If you want to use 3 view controllers instead of a scroll view with 3 pages, this simple implementation using a tab bar controller should work for you. I started with the tabbed application template, and added a third view controller. The controllers at indexes 0, 1, and 2 correspond to your views A, B, and Z respectively. In the viewDidLoad method of the A controller, I set the tab bar to be hidden.
I created a BaseViewController class that the 3 controllers I setup in the storyboard inherit from. The code in BaseViewController creates and adds the swipe gesture recognizers, handles the swipes in such a way that gives you the rotating sequence, and gives you a slide over transition between the views,
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swiperRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperRight];
UISwipeGestureRecognizer *swiperLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swiperLeft];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
if (sender.direction == 1) {
NSInteger nextIndex = (self.tabBarController.selectedIndex - 1 == -1)? 2 : self.tabBarController.selectedIndex - 1;
[self slideInViewWithIndex:nextIndex direction:-1];
}else{
NSInteger nextIndex = (self.tabBarController.selectedIndex + 1 == 3)? 0 : self.tabBarController.selectedIndex + 1;
[self slideInViewWithIndex:nextIndex direction:1];
}
}
-(void)slideInViewWithIndex:(NSInteger) index direction:(NSInteger) dir {
UIView *nextView = [(self.tabBarController.viewControllers[index]) view];
[self.tabBarController.view addSubview:nextView];
nextView.frame = CGRectOffset(self.tabBarController.view.bounds, dir * self.tabBarController.view.bounds.size.width, 0);
[UIView animateWithDuration:.3 animations:^{
nextView.frame = self.tabBarController.view.bounds;
} completion:^(BOOL finished) {
self.tabBarController.selectedIndex = index;
}];
}
Apple sample code StreetScroller
Github repositories:
by Daniele Margutti DMCircularScrollView
by Benjamin Guest BAGPagingScrollView
A tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With