从左到右推动动画

时间:2013-04-19 03:43:21

标签: uiview uiviewcontroller uistoryboardsegue uiviewanimationtransition

我正在使用这种方法:

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

控制segue上的后退动画。准确使用此代码:

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

这有效,但我真正想要的不是翻转,它是推。换句话说,我希望看到视图从右向左滑动,一个替换另一个。

不幸的是,没有UIViewAnimationOptionTransitionPushFromRight。

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

1 个答案:

答案 0 :(得分:0)

如果您使用的是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];
                     }];   
}