【问题标题】:Using UIScrollView to "rotate" between three views使用 UIScrollView 在三个视图之间“旋转”
【发布时间】:2014-07-17 02:13:46
【问题描述】:

我有一个UIScrollView,我添加了三个视图ZAB

我想要完成的是以“旋转”方式设置三个视图,以首先出现的视图 A 为中心。当用户向左滑动并看到视图B 时,再次向左滑动向左 可以看到Z,反之亦然,在视图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 上滑动到Z),希望能提供任何帮助。

谢谢!

【问题讨论】:

  • 您是否考虑过搜索transitionWithViewUIView animate... 方法的示例?还有一些不错的文章,例如ViewControllerTransitions
  • 感谢@andrewbuilder 的评论。只是想确保我理解 - 你的建议不是设置一个实际的 UIScrollView 内部视图,而是我只有一个 UIView 只是基于滑动动画到不同的内容?
  • 免责声明:我之前没有尝试过,所以这些只是建议......是的,我想知道您是否有三个视图控制器可以在它们之间转换。有很多方法可以自定义视图转换。如果视图是三个独立的视图控制器,这可能更容易编码和稍后管理。否则,您可能正在考虑为滚动视图编写自定义动画。并不是说它不能完成,也许更难编码和管理。
  • 我同意@andrewbuilder,我会使用 3 个控制器并为它们之间的过渡设置动画。我认为使用 UITabBarController (隐藏标签栏)将是一个很好的方法,因为它保留了指向所有控制器的指针(所以当你在它们之间移动时,你每次都会去同一个实例)。此外,能够使用 setSelectedIndex: 可以轻松转到所需的视图。

标签: ios objective-c uiviewcontroller uiscrollview


【解决方案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;
    }];
}

【讨论】:

    【解决方案2】:

    苹果示例代码StreetScroller

    Github 存储库:

    教程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      相关资源
      最近更新 更多