在自定义弹出segue期间,视图过渡不会生成动画

时间:2012-10-23 17:28:39

标签: ios uistoryboardsegue

当我将新视图控制器推送到导航堆栈时,我正在使用自定义segue来创建展开动画。如果在调用prepareForSegue:sender:时未指定sourceRect,则目标视图控制器将从源视图控制器的中心展开。

ExpandSegue.m:

- (id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination {
    self = [super initWithIdentifier:identifier source:source destination:destination];

    self.sourceRect = CGRectMake(source.view.center.x, source.view.center.y, 0.0, 0.0);

    return self;
}

- (void)perform {
    UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;

    CGRect destinationRect = sourceViewController.view.frame;
    CGFloat tx = self.sourceRect.origin.x + self.sourceRect.size.width/2 - destinationRect.origin.x - destinationRect.size.width/2;
    CGFloat ty = self.sourceRect.origin.y + self.sourceRect.size.height/2 - destinationRect.origin.y - destinationRect.size.height/2;
    CGFloat sx = self.sourceRect.size.width/destinationRect.size.width;
    CGFloat sy = self.sourceRect.size.height/destinationRect.size.height;

    [sourceViewController.view addSubview:destinationViewController.view];
    [destinationViewController.view setFrame:sourceViewController.view.frame];
    [destinationViewController.view setTransform:CGAffineTransformConcat(CGAffineTransformMakeScale(sx, sy), CGAffineTransformMakeTranslation(tx, ty))];

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         [destinationViewController.view setTransform:CGAffineTransformIdentity];
                     } completion:^(BOOL finished) {
                         [destinationViewController.view removeFromSuperview];
                         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                     }];
}

expand segue按预期工作。我试图通过从扩展segue中反转逻辑来创建崩溃segue,但这给了我一些问题。我希望源视图控制器缩小到destinationRect的大小。如果没有提供destinationRect,那么我希望视图缩小到目标视图控制器视图的中心。

CollapseSegue.m:

- (id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination {
    self = [super initWithIdentifier:identifier source:source destination:destination];

    self.destinationRect = CGRectMake(source.view.center.x, source.view.center.y, 0.0, 0.0);

    return self;
}

- (void)perform {
    UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;

    CGRect sourceRect = sourceViewController.view.frame;
    CGFloat tx = self.destinationRect.origin.x + self.destinationRect.size.width/2 - sourceRect.origin.x - sourceRect.size.width/2;
    CGFloat ty = self.destinationRect.origin.y + self.destinationRect.size.height/2 - sourceRect.origin.y - sourceRect.size.height/2;
    CGFloat sx = self.destinationRect.size.width/sourceRect.size.width;
    CGFloat sy = self.destinationRect.size.height/sourceRect.size.height;

    [sourceViewController.navigationController popViewControllerAnimated:NO];
    [destinationViewController.view addSubview:sourceViewController.view];
    [sourceViewController.view setFrame:destinationViewController.view.frame];

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         [sourceViewController.view setTransform:CGAffineTransformConcat(CGAffineTransformMakeScale(sx, sy), CGAffineTransformMakeTranslation(tx, ty))];
                     } completion:^(BOOL finished) {
                         [sourceViewController.view removeFromSuperview];
                     }];
}

而不是动画很好,sourceViewController的视图只是消失,好像我只调用了[sourceViewController.navigationController popViewControllerAnimated:NO];这似乎表明我没有正确设置我的视图层次结构,但我似乎无法弄清楚我哪里错了!任何帮助将不胜感激。

0 个答案:

没有答案