presentViewController过渡动画

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了presentViewController过渡动画相关的知识,希望对你有一定的参考价值。

在我的代码中,我使用presentViewController来调用我的第二个viewcontroller

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

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

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

答案

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

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

// Take a look at this enum

typedef enum {
   UIModalTransitionStyleCoverVertical = 0,
   UIModalTransitionStyleFlipHorizontal,
   UIModalTransitionStyleCrossDissolve,
   UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
另一答案

对斯威夫特来说

定义动画类型


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

如何使用


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

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

1.创建自定义转换。

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

履行

@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.创建过渡代表。

@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

使用样本。

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

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

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

此代码适用于ios 10+。

另一答案

所有上述内容也可以在故事板中实现,而无需编写代码。单击链接视图控制器的segue,然后选择右侧菜单中的Attributes Inspector选项卡。在Kind下拉菜单中选择'Present Modally'。将出现另一组选项。在“转换”下拉菜单中,您可以选择上面列出的任何枚举。 Segue Attributes Inspector

另一答案

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

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

以上是关于presentViewController过渡动画的主要内容,如果未能解决你的问题,请参考以下文章

presentViewController过渡动画

带有自定义过渡的 presentViewController

presentViewController 的动画属性

presentViewController:动画:完成:失败

PresentViewController:动画不显示在 iPad 上

UIViewController presentViewController modal在iOS7中没有动画