使用UIScrollView在三个视图之间“旋转”

时间:2014-05-28 02:04:40

标签: ios objective-c uiviewcontroller uiscrollview

我有一个UIScrollView我已添加了三个观看次数ZAB

enter image description here

我想要完成的是将三个视图设置为"旋转"首先出现在视图A上的方式。当用户向左滑动并看到视图B时,再次向向左滑动可以查看Z,反之亦然,在浏览时向右滑动向右 {1}}让用户查看Z

我已经设置了B的代码:

self.scrollview

然而,目前我不确定如何继续进行"轮换"要工作的元素(即在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; 上滑动到B)并感谢任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您想使用3个视图控制器而不是3页的滚动视图,那么使用标签栏控制器的这个简单实现应该适合您。我从选项卡式应用程序模板开始,并添加了第三个视图控制器。索引0,1和2处的控制器分别对应于您的视图A,B和Z.在A控制器的viewDidLoad方法中,我将标签栏设置为隐藏。

我创建了一个BaseViewController类,我在故事板中设置的3个控制器继承自。 BaseViewController中的代码创建并添加滑动手势识别器,以这样一种方式处理滑动,为您提供旋转序列,并为您提供视图之间的过渡滑动,

@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;
    }];
}

答案 1 :(得分:1)

Apple示例代码StreetScroller

Github存储库:

教程