在解除视图控制器时我需要自定义segue

时间:2015-06-10 04:56:36

标签: ios objective-c

我想解雇当前的viewController,我需要自定义segue来实现它。

- (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *sourceTabBarController = sourceViewController.parentViewController.parentViewController;
    UIViewController *destinationViewController = self.destinationViewController;
    UIGraphicsBeginImageContext(destinationViewController.view.bounds.size);
    [destinationViewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *destinationViewControllerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *destinationViewControllerImageView = [[UIImageView alloc] initWithImage:destinationViewControllerImage];
    destinationViewControllerImageView.userInteractionEnabled = YES;
    destinationViewControllerImageView.frame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(destinationViewController.view.frame), CGRectGetHeight(destinationViewController.view.frame));

    [destinationViewController.view insertSubview:destinationViewControllerImageView atIndex:1];

    // Add animations
    [UIView animateWithDuration:0.4f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         destinationViewControllerImageView.center = CGPointMake(-CGRectGetWidth(destinationViewControllerImageView.frame) / 2, -(CGRectGetHeight(destinationViewControllerImageView.frame) / 2));
                     }
                     completion:^(BOOL finished){
                         [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];
                     }];


}

它工作正常,但没有动画:(

2 个答案:

答案 0 :(得分:1)

你在这里设置动画为NO - 可能是问题在这里

[[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];

将其设置为动画:是

答案 1 :(得分:0)

问题不在于将animated:设置为NO,而是因为动画正在被中断;因此,presentViewController内的completion block会立即被调用。

相反,请尝试将presentViewController:代码包装在if - 子句中,如下所示:

if(finished)
{
     [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];
}

由于您的动画持续时间仅为0.4f,因此您无法真正看到它,因为它发生的速度非常快。尝试将持续时间设置得更长,然后您可以在调用 destinationViewControllerImageView之前看到presentViewController动画到其位置

我用动画opacity进行了快速测试;它应该按预期工作。

希望这会有所帮助。