presentViewController 过渡动画

Posted

技术标签:

【中文标题】presentViewController 过渡动画【英文标题】:presentViewController transition animation 【发布时间】:2015-03-29 14:26:30 【问题描述】:

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

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

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

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

【问题讨论】:

【参考方案1】:

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

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

// Take a look at this enum

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

【讨论】:

不,它不起作用,它只是没有动画。 secondController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // 看看这个枚举 typedef enum UIModalTransitionStyleCoverVertical = 0, UIModalTransitionStyleFlipHorizo​​ntal, UIModalTransitionStyleCrossDissolve, UIModalTransitionStylePartialCurl, UIModalTransitionStyle; [self presentViewController:secondController 动画:YES 完成:nil]; 我给了这样的但它没有工作 secondController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;; secondController.modalPresentationStyle = UIModalPresentationFullScreen; ; // 看看这个枚举 typedef enum UIModalTransitionStyleCoverVertical = 0, UIModalTransitionStyleFlipHorizo​​ntal, UIModalTransitionStyleCrossDissolve, UIModalTransitionStylePartialCurl, UIModalTransitionStyle; [self presentViewController:secondController 动画:YES 完成:nil]; @SubramanianRaj 你在调用 [UIViewController presentViewController:animated:completion] 之前设置了这些属性吗? UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil]; HomeViewController *secondController = (HomeViewController *)[storyboard instantiateViewControllerWithIdentifier:@"test"]; secondController.LoginID=一个ID; secondController.LoginUsername= 用户名; secondController.UserProfileImage=ProfileImage; secondController.UserOtherInfo=用户信息; @SubramanianRaj 好吧,将您的演示文稿样式更改为 UIModalPresentationStyleOverCurrentContext【参考方案2】:

对于斯威夫特

定义动画类型


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)

【讨论】:

【参考方案3】:

我决定使用 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+。

【讨论】:

在我看来是最好的解决方案。就像导航控制器转换一样工作。谢谢。 根据 Apple 在developer.apple.com/library/archive/featuredarticles/… 的文档,您应该使用 viewForKey 来添加或删除视图,而不是视图控制器的视图属性。【参考方案4】:

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

【讨论】:

【参考方案5】:

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

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

【讨论】:

这只是这个问题的另一个答案的副本。请仅在您有新内容要添加时添加答案。

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

presentViewController过渡动画

带有自定义过渡的 presentViewController

Vue过渡动效

UINavigationController 上视图之间的过渡动​​画有多长?

Fancybox 3 IFrame 内容之间的过渡动​​画效果

vue过渡动效