2

我正在使用这种方法:

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

控制转场上的后退动画。精确使用此代码:

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

那行得通,但我真正想要的不是翻转,而是推送。换句话说,我希望看到视图从右向左滑动,一个替换另一个。

不幸的是,没有 UIViewAnimationOptionTransitionPushFromRight。

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

4

1 回答 1

0

如果您使用的是导航控制器,则可以执行以下操作:

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

如果您不想使用 a UINavigationController,并且您希望源视图控制器消失而不是 using presentViewController,并且您不希望由 using 引起的淡出动画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];
                     }];   
}
于 2013-07-30T21:47:44.533 回答