好吧,一旦您的视图显示出来,您就可以在其中制作几乎任何您想要的动画。你可以做一个简单的[UIView animateWithDuration] 类型的交易,但我个人会使用CATransition,它相对简单。
QuartzCore 之道
首先,我假设您呈现的视图是透明的,并且内部还有另一个视图,其行为类似于对话框。将要呈现的视图控制器,我们称之为PresentedViewController,并为其中的视图保存dialog 属性。
PresentedViewController.m
(需要链接到QuartzCore.h)
#import <QuartzCore/QuartzCore.h>
@implementation PresentedViewController
- (void)viewWillAppear:(BOOL)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"];
在这里,视图将通过平移移出屏幕,然后旋转,然后滑入,回到原来的变换。我还修改了计时功能以提供更平滑的曲线。
考虑一下,我只是对 CoreAnimation 的可能性进行了初步了解,我已经在这条道路上工作了三年,并且我已经成长为喜欢 CAAnimation 所做的所有事情。
故事板专业提示:如果您构建自己的自定义 UIStoryboardSegue 子类,您可以很好地完成此操作。