1

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

展开Segue.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];
                     }];
}

扩展转场按预期工作。我试图通过反转展开转场的逻辑来创建折叠转场,但这给我带来了问题。我希望源视图控制器缩小到目标矩形的大小。如果没有提供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];这似乎表明我没有正确设置视图层次结构,但我似乎无法弄清楚我哪里出错了!任何帮助将不胜感激。

4

0 回答 0