【问题标题】:iOS - Custom Transion With Container View Controller causing problemsiOS - 使用容器视图控制器的自定义转换导致问题
【发布时间】:2015-04-12 01:17:10
【问题描述】:

我有一个容器视图控制器,用于控制两个子视图控制器之间的转换。动画和过渡按我的意图工作。我单击按钮,容器中的视图控制器与另一个具有漂亮自定义动画的子视图控制器交换。

但是,如果我在过渡动画正在进行时单击我的按钮,它将搞砸视图控制器的呈现。

即使在交换之间的动画正在进行时,如何让我的视图控制器交换方法工作?

这是我在容器视图控制器中交换 VC 的代码。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:SegueIdentifierFirst])
    {
        if (self.childViewControllers.count > 0) {
            [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController];
        }
        else {
            [self addChildViewController:segue.destinationViewController];
            ((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            [self.view addSubview:((UIViewController *)segue.destinationViewController).view];
            [segue.destinationViewController didMoveToParentViewController:self];
        }
    }
    else if ([segue.identifier isEqualToString:SegueIdentifierSecond])
    {
        [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController];
    }
}

- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    toViewController.view.frame = CGRectMake(0, 560, width, height);
    [self transitionFromViewController:fromViewController toViewController:toViewController duration:.2 options:UIViewAnimationOptionCurveLinear animations:^{
        fromViewController.view.frame = CGRectMake(0, 560, width, height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            toViewController.view.frame = CGRectMake(0, 0, width, height);
        } completion:^(BOOL finished) {
            [fromViewController removeFromParentViewController];
            [toViewController didMoveToParentViewController:self];
        }];
    }];
}

- (void)swapViewControllers
{
    self.currentSegueIdentifier = ([self.currentSegueIdentifier  isEqual: SegueIdentifierFirst]) ? SegueIdentifierSecond : SegueIdentifierFirst;
    [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
}

这是用于在我的主视图控制器中交换的按钮。

- (IBAction)playlistsButtonPressed:(UIButton *)sender
{
    [self.containerViewController swapViewControllers];

    if ([self.playlistsButton.titleLabel.text isEqualToString:@"PLAYLISTS"]) {
        [self.playlistsButton setTitle:@"SEARCH" forState:UIControlStateNormal];
    }
    else{
        [self.playlistsButton setTitle:@"PLAYLISTS" forState:UIControlStateNormal];
    }
}

这甚至可能吗?还是我必须在动画进行时禁用/启用我的按钮?如果是这样,我应该怎么做?

【问题讨论】:

    标签: ios objective-c uiviewcontroller


    【解决方案1】:

    此代码似乎可以解决问题。不确定这是否是解决此问题的正确方法。

    我将从父视图控制器中删除 fromViewController 的位置更改为动画块的开头。

     [self transitionFromViewController:fromViewController toViewController:toViewController duration:.2 options:UIViewAnimationOptionCurveLinear animations:^{
            [fromViewController removeFromParentViewController];
            fromViewController.view.frame = CGRectMake(0, 560, width, height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                toViewController.view.frame = CGRectMake(0, 0, width, height);
            } completion:^(BOOL finished) {
                [toViewController didMoveToParentViewController:self];
            }];
        }];
    

    【讨论】:

      【解决方案2】:

      最好设置:

         - (void)swapViewControllers
      {
      
        if (self.transitionInProgress) {
          return;
      }
      
        // YOUR CODE
      
      }
      

      transitionInProgress 是一个 BOOLEAN 值,开头设置为 NO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-25
        相关资源
        最近更新 更多