ios presentViewController 过渡动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29330305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:24:34  来源:igfitidea点击:

presentViewController transition animation

iosobjective-cuiviewcontrollernavigationcontrollerpresentviewcontroller

提问by Subramanian Raj

In my code I am using presentViewController to call my second viewcontroller

在我的代码中,我使用 presentViewController 来调用我的第二个视图控制器

[self presentViewController:secondController animated:YES completion:nil];

When I call I need to show left to right animation (like in navigationController)

当我打电话时,我需要显示从左到右的动画(如在 navigationController 中)

I don't want to use the navigationController but I need the animation similar to navigationController in presentViewController...

我不想使用 navigationController 但我需要类似于 presentViewController 中的 navigationController 的动画...

回答by Ch0k0l8

Add this line of code before presenting view controller

在呈现视图控制器之前添加这行代码

secondController.modalTransitionStyle   = UIModalTransitionStyleCrossDissolve;
secondController.modalPresentationStyle = UIModalPresentationFullScreen;

// Take a look at this enum

typedef enum {
   UIModalTransitionStyleCoverVertical = 0,
   UIModalTransitionStyleFlipHorizontal,
   UIModalTransitionStyleCrossDissolve,
   UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

回答by swiftBoy

for Swift

对于斯威夫特

Define animations types

定义动画类型



enum UIModalTransitionStyle : Int {
    case CoverVertical = 0
    case FlipHorizontal
    case CrossDissolve
    case PartialCurl
}

How to use

如何使用



let vc : PageHomeViewController = storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") as! PageHomeViewController
vc.modalTransitionStyle = .FlipHorizontal
self.presentViewController(vc, animated: true, completion: nil)

回答by Илья Еловиков

My decision for resolving the animation "cover horizontal" like a UINavigationViewController push method with using UIViewControllerTransitioningDelegate.

我决定使用 UIViewControllerTransitioningDelegate 像 UINavigationViewController 推送方法一样解决动画“水平覆盖”。

1.Create a custom transition.

1.创建自定义过渡。

Header

标题

@interface CoverHorizontalTransition: NSObject<UIViewControllerAnimatedTransitioning>
@property (assign, nonatomic) BOOL dismiss;
@end

Implementation

执行

@implementation CoverHorizontalTransition

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController;
    fromViewController =
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    UIViewController *toViewController;
    toViewController =
    [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = transitionContext.containerView;

    CGRect animatedViewFrame;
    animatedViewFrame = containerView.bounds;
    animatedViewFrame.origin = CGPointMake(CGRectGetWidth(animatedViewFrame), 0);

    [containerView addSubview:toViewController.view];

    if (_dismiss) {
        [containerView bringSubviewToFront:fromViewController.view];

        [UIView
         animateWithDuration:[self transitionDuration:transitionContext]
         animations:^{
             fromViewController.view.frame = animatedViewFrame;
         } completion:^(BOOL finished) {
             [containerView.superview addSubview:toViewController.view];
             [fromViewController.view removeFromSuperview];
             [transitionContext completeTransition:YES];
         }];
    } else {
        toViewController.view.frame = animatedViewFrame;

        [UIView
         animateWithDuration:[self transitionDuration:transitionContext]
         animations:^{
             toViewController.view.center = containerView.center;
         } completion:^(BOOL finished) {
             [transitionContext completeTransition:YES];
         }];
    }
}

- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.25;
}

@end

2.Create transition delegate.

2.创建过渡委托。

@implementation CustomViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [CoverHorizontalTransition new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    CoverHorizontalTransition *transition;
    transition = [CoverHorizontalTransition new];
    transition.dismiss = YES;

    return transition;
}

@end

Sample of using.

使用示例。

...
// Save delegate to strong property
secondController.customTransitioningDelegate =
[BaseViewControllerTransitioningDelegate new];

secondController.transitioningDelegate =
secondController.customTransitioningDelegate;
secondController.modalPresentationStyle = UIModalPresentationCustom;

[self presentViewController:secondController animated:YES completion:nil];

This code works for iOS 10+.

此代码适用于 iOS 10+。

回答by elarctheitroadis

All of the above can also be achieved in storyboard without having to write code. Click on your segue that links your view controllers, then select the Attributes Inspector tab in the right side menu. In the Kind drop down menu select 'Present Modally'. Another set of options will come up. In the Transition drop down menu you will be able to select any of the enums listed above. Segue Attributes Inspector

以上所有内容也可以在storyboard中实现,无需编写代码。单击链接视图控制器的 segue,然后在右侧菜单中选择 Attributes Inspector 选项卡。在种类下拉菜单中选择“以模态呈现”。将出现另一组选项。在 Transition 下拉菜单中,您可以选择上面列出的任何枚举。Segue 属性检查器

回答by Keshu R.

@swiftboy answer is most correct. You don't need to declare the enum and you can call it directly.

@swiftboy 答案是最正确的。您不需要声明枚举,您可以直接调用它。

let vc : PageHomeViewController = 
storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") 
as! PageHomeViewController
vc.modalTransitionStyle = .FlipHorizontal 
self.presentViewController(vc, animated: true, completion: nil)