【发布时间】: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