我有一个容器视图控制器,用于控制两个子视图控制器之间的转换。动画和过渡正如我所愿。我单击我的按钮,我的容器中的视图控制器与另一个子视图控制器交换了一个很好的自定义动画。
但是,如果我在转换动画进行过程中单击我的按钮,它将搞砸视图控制器的显示。
即使交换之间的动画正在进行中,如何允许交换视图控制器方法?
以下是我在容器视图控制器中交换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];
}
}
这甚至可能吗?或者我将不得不在动画进行过程中禁用/启用我的按钮?如果是这样,我该怎么做呢?
答案 0 :(得分:0)
此代码似乎可以解决问题。不确定这是否是解决这个问题的正确方法。
我改变了将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];
}];
}];
答案 1 :(得分:0)
最好设置:
- (void)swapViewControllers
{
if (self.transitionInProgress) {
return;
}
// YOUR CODE
}
其中transitionInProgress是BOOLEAN值,在开头
中设置为NO