Segue 与 Push 动画从右到左

Posted

技术标签:

【中文标题】Segue 与 Push 动画从右到左【英文标题】:Segue with Push animation Right to Left 【发布时间】:2013-04-19 03:43:21 【问题描述】:

我正在使用这种方法:

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

控制转场上的后退动画。正是使用这段代码:

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

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

遗憾的是没有 UIViewAnimationOptionTransitionPushFromRight。

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于Segue 与 Push 动画从右到左的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController:从右到左的语言反转 .scroll 动画方向

Android从右到左textview滑动动画

移动按钮从右到左动画iOS - swift

UICollectionView:1 行水平滚动(从右到左),带有来自右侧的新单元格的自定义动画

在无限循环中从左到右,从右到左平移动画箭头图像

如何制作点击按钮时显示的加载动画?它应该占据全屏并且应该从右到左并消失