【问题标题】:How to slide child view controllers contained inside a UIViewController in iOS?如何在 iOS 中滑动 UIViewController 中包含的子视图控制器?
【发布时间】:2015-07-14 04:33:16
【问题描述】:

这是我当前的屏幕:

此屏幕由 UIViewController 组成,其中包含以编程方式创建的按钮 Current BookingsOld Bookings。我有关于如何通过将 UIView 作为子视图添加到 UIButton 来使点击处于活动状态的逻辑。

按钮下方的所有空间都被一个容器视图占据,我想根据哪个按钮处于活动状态添加不同的 UIViewControllers。

最初,我这样做是为了在父视图出现时加载其中一个视图控制器,

DRMCurrentBookingsViewController *cbvc = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
[self addChildViewController:cbvc];
// 64 is the height of the navigation bar
cbvc.view.frame = CGRectMake(0, 64+50, self.view.bounds.size.width, self.view.bounds.size.height-50);
[container addSubview:cbvc.view];
[cbvc didMoveToParentViewController:self];

然后,当用户点击不同的按钮时,我使用此代码来切换视图。

DRMCurrentBookingsViewController *newController = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
DRMOldBookingsViewController *oldController = [self.childViewControllers objectAtIndex:0];

        [oldController willMoveToParentViewController:nil];
        [self addChildViewController:newController];

        [self transitionFromViewController:oldController
                          toViewController:newController
                                  duration:0.5
                                   options:UIViewAnimationOptionTransitionNone
                                animations:^{
                                    // no further animations required
                                    newController.view.frame = oldController.view.frame;
                                }
                                completion:^(BOOL finished) {
                                    [oldController removeFromParentViewController]; 
                                    [newController didMoveToParentViewController:self];
                                }];

但是,我希望子视图根据点击的按钮从左右或左右滑动,我不想选择预定义的UIViewAnimationOption我什至不想消失在视图中。视图必须从一端滑到另一端。

我怎样才能做到这一点?

感谢您的帮助。

【问题讨论】:

  • 你想要推送 viewController 或添加 subView,如果你想要添加 viewController 它不会显示在两个按钮上方,所以我认为你将添加 subview 作为另一个 viewController 视图
  • 你想向哪个方向移动子视图?

标签: ios objective-c iphone childviewcontroller


【解决方案1】:

知道了。我就是这样做的:

DRMCurrentBookingsTableViewController *newController = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
    DRMOldBookingsTableViewController *oldController = [self.childViewControllers objectAtIndex:0];

    [oldController willMoveToParentViewController:nil];
    [self addChildViewController:newController];

    // I added these
    CGRect newFrame = container.frame;
    newFrame.origin.x -= self.view.bounds.size.width;
    newFrame.size.width = self.view.bounds.size.width;
    newController.view.frame = newFrame;
    newFrame = container.frame;
    newFrame.size.width = self.view.bounds.size.width;

    [self transitionFromViewController:oldController
                      toViewController:newController
                              duration:0.25
                               options:UIViewAnimationOptionCurveEaseInOut
                            animations:^{
                                // no further animations required
                                newController.view.frame = newFrame;
                            }
                            completion:^(BOOL finished) {
                                [oldController removeFromParentViewController];
                                [newController didMoveToParentViewController:self];
                            }];

感谢大家的帮助。

【讨论】:

    【解决方案2】:

    我不认为“从左到右”和“从右到左”可用作苹果动画中的构建。我认为您应该使用 2 个容器视图并使用 [UIView animationWithDuration: 方法来创建“从左到右”动画

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多