1

新しいViewControllerをナビゲーションスタックにプッシュするときに、カスタムセグエを使用して拡張アニメーションを作成しています。が呼び出されたときにsourceRectが指定されていない場合、宛先ViewControllerはソースViewControllerprepareForSegue:sender:の中心から展開されます。

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];
                     }];
}

エキスパンドセグエは期待どおりに機能します。エキスパンドセグエのロジックを逆にして折りたたみセグエを作成しようとしましたが、問題が発生しました。ソースビューコントローラを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];これは、ビュー階層が適切に設定されていないことを示しているように見えますが、どこが間違っているのか理解できないようです。 !!どんな助けでも大歓迎です。

4

0 に答える 0