OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 UIModalPresentationCurrentContext 后如何为 View 设置动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:15
标题: ios - 使用 UIModalPresentationCurrentContext 后如何为 View 设置动画

当我遇到 this post 时,我正在寻找有关如何在 iOS 对话框中包装 View 的解决方案。 ,其中有这一行:

vc.modalPresentationStyle = UIModalPresentationCurrentContext;

它基本上解决了我创建/模仿对话框的问题,但它不会像帖子中提到的那样在过渡时设置动画。那么获得上滑动画最简单的方法是什么?

ps.我会在该帖子中将此作为子问题提出,但我没有 50 条代表评论



Best Answer-推荐答案


好吧,一旦你的 View 被显示出来,你几乎可以在其中做任何你想要的动画。你可以做一个简单的 [UIView animateWithDuration] 类的交易,但我个人会使用 CATransition 来做这件事,它相对简单。

QuartzCore 之道

首先,我假设您呈现的 View 是透明的,并且内部还有另一个 View ,其行为类似于对话框。将要呈现的 View Controller ,我们称它为 PresentedViewController 并保存其中 View 的 dialog 属性。

PresentedViewController.m

(需要链接到 QuartzCore.h)

#import <QuartzCore/QuartzCore.h>

@implementation PresentedViewController

- (void)viewWillAppearBOOL)animated
{
    [super viewWillAppear:animated];

    if (animated)
    {
        CATransition *slide = [CATransition animation];

        slide.type = kCATransitionPush;
        slide.subtype = kCATransitionFromTop;
        slide.duration = 0.4;
        slide.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

        slide.removedOnCompletion = YES;

        [self.dialog.layer addAnimation:slide forKey"slidein"];
    }
}

变得花哨

这样做的好处是您可以创建自己的自定义动画,并使用其他属性。

CABasicAnimation *animations = [CABasicAnimation animationWithKeyPath"transform"];

CATransform3D transform;

// Take outside the screen
transform = CATransform3DMakeTranslation(0, self.view.bounds.size.height, 0);

// Rotate it
transform = CATransform3DRotate(transform, M_PI_4, 0, 0, 1);

animations.fromValue    = [NSValue valueWithCATransform3D:transform];
animations.toValue      = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animations.duration     = 0.4;
animations.fillMode     = kCAFillModeForwards;
animations.removedOnCompletion = YES;
animations.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0 :0.0 :0 :1];

[self.dialog.layer addAnimation:animations forKey"slidein"];

在这里, View 将通过平移移出屏幕,然后旋转,然后滑入,回到原来的变换。我还修改了计时功能以提供更平滑的曲线。

考虑到我只是对 CoreAnimation 的可能性进行了初步了解,我已经在这条道路上工作了三年,并且我已经成长为喜欢 CAAnimation 所做的所有事情。

Storyboard 专业提示:如果您构建自己的自定义 UIStoryboardSegue 子类,您可以很好地完成此操作。

关于ios - 使用 UIModalPresentationCurrentContext 后如何为 View 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17937347/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4