Uiview 从右向左推送动画的顺序

Uiview 从右向左推送动画的顺序,uiview,uiviewcontroller,uistoryboardsegue,uiviewanimationtransition,Uiview,Uiviewcontroller,Uistoryboardsegue,Uiviewanimationtransition,我使用的方法是: [UIView transitionWithView: duration: options: animations: completion:]; 控制序列上的反向动画。准确地使用此代码: [UIView transitionWithView:self.view.superview duration:2.0 options:UIViewAnimationOptionTransitionFlipFromRi

我使用的方法是:

[UIView transitionWithView: duration: options: animations: completion:];
控制序列上的反向动画。准确地使用此代码:

[UIView transitionWithView:self.view.superview
                  duration:2.0
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    [self dismissModalViewControllerAnimated:NO];
                }
                completion:nil];
这是可行的,但我真正想要的不是翻转,而是推动。换句话说,我希望看到视图从右向左滑动,一个替换另一个

不幸的是,没有UIViewAnimationOptionTransitionPushFromRight


因此,我的问题是:如何获得我想要的效果?

如果您使用的是navigationController,您可以执行以下操作:

- (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
导致动画淡出,则以下解决方案将起作用。如果您的视图是透明的,并且您不希望包含的背景设置动画,则此操作也有效

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