【问题标题】:Segue with Push animation Right to LeftSegue 与 Push 动画从右到左
【发布时间】: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。

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

【问题讨论】:

    标签: uiview uiviewcontroller uistoryboardsegue uiviewanimationtransition


    【解决方案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];
                         }];   
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2013-12-28
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多