本文介绍了带有从右到左推送动画的 Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种方法:

[UIView transitionWithView: duration: options: animations: completion:];

控制转场上的后退动画.正是使用此代码:

to control a back animation on a segue. Precisely using this code:

[UIView transitionWithView:self.view.superview
                  duration:2.0
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    [self dismissModalViewControllerAnimated:NO];
                }
                completion:nil];

这可行,但我真正想要的不是 Flip,而是 Push.换句话说,我希望看到视图从右向左滑动,一个替换另一个.

That works, but what I really want is not a Flip, it is a Push. In other words I want to see the views sliding from right to left, one replacing the other.

遗憾的是没有 UIViewAnimationOptionTransitionPushFromRight.

Unfortunately there is no UIViewAnimationOptionTransitionPushFromRight.

所以我的问题是:我怎样才能得到我想要的效果?

Therefore my question: how can I get the effect I want?

推荐答案

如果你使用了 navigationController,你可以这样做:

If you are using a navigationController, you can do something like this:

- (void)perform
{
    UIViewController *source = (UIViewController*)[self sourceViewController];
    UIViewController *destination = (UIViewController*)[self destinationViewController];

    CATransition* transition = [CATransition animation];
    transition.duration = .25;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;

    [source.navigationController.view.layer addAnimation:transition
                                                  forKey:kCATransition];

    [source.navigationController pushViewController:destination animated:NO];
}

如果您不想使用 UINavigationController,并且您希望源视图控制器消失而不是使用 presentViewController,并且您不希望使用 kCATransitionPush 引起的淡出动画,那么下面的解决方案将起作用.如果您的视图是透明的并且您不希望包含背景动画,这也适用.

If you don't want to use a UINavigationController, and you want the source view controller to go away as opposed to using presentViewController, and you don't want the fade out animation caused by using kCATransitionPush, then the following solution will work. This also works if your views are transparent and you do not want the containing background to animate.

static UIImageView *screenShotOfView(UIView *view)
{
    // Create a snapshot for animation
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImageView *screenShot = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    UIGraphicsEndImageContext();
    return screenShot;
}

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

    // Swap the snapshot out for the source view controller
    UIWindow *window = source.view.window;
    UIImageView *screenShot = screenShotOfView(source.view);
    CGRect originalFrame = destination.view.frame;

    BOOL animsEnabled = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    {
        window.rootViewController = destination;
        [window addSubview:screenShot];
        [source.view removeFromSuperview];
        CGRect frame = destination.view.frame;
        frame.origin.x += source.view.bounds.size.width;
        destination.view.frame = frame;
    }
    [UIView setAnimationsEnabled:animsEnabled];

    [UIView animateWithDuration:kAnimationDuration
                     animations:^{
                         destination.view.frame = originalFrame;
                         CGRect frame = screenShot.frame;
                         frame.origin.x -= screenShot.bounds.size.width;
                         screenShot.frame = frame;

                     }
                     completion:^(BOOL finished) {
                         [screenShot removeFromSuperview];
                     }];   
}

这篇关于带有从右到左推送动画的 Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 10:48