TYAlertController转场动画指南:Fade、ScaleFade、DropDown三种动画详解与切换技巧
TYAlertController转场动画指南:Fade、ScaleFade、DropDown三种动画详解与切换技巧
TYAlertController 是一款强大的 iOS Objective-C 弹窗库,支持在 controller 和 window 上弹出 alertView / popupView,并提供模糊背景(blur effects)、自定义视图和转场动画。本文带你快速搞懂内置的 Fade、ScaleFade、DropDown 三种转场动画的差异与切换技巧,让你的弹窗动效一步到位。
一分钟看懂三种转场动画
先上效果参考图(Demo 中的弹窗背景效果):
TYAlertController 内置动画由统一的枚举 TYAlertTransitionAnimation 定义(见 TYAlertController.h),共 4 个选项:Fade、ScaleFade、DropDown、Custom。三者核心差异如下:
| 动画 | 入场效果(Alert 样式) | 出场效果 | 时长 |
|---|---|---|---|
| Fade(默认) | 从 0.5 倍放大,两段式回弹 | 缩小至 0.9 倍 + 淡出 | 入场 0.45s / 出场 0.25s |
| ScaleFade | 从 0.1 倍平滑放大到 1 倍 | 缩小至 0.1 倍 + 淡出 | 0.3s |
| DropDown | 从屏幕顶部上方"掉落"落下,带弹簧阻尼 | 缩小 + 淡出 | 入场 0.5s(弹簧)/ 出场 0.25s |
1️⃣ Fade:默认的淡入淡出动画
TYAlertFadeAnimation 是缺省动画——不指定任何参数创建控制器时就会使用它(见 TYAlertController.m)。
它的细节在 TYAlertFadeAnimation.m 中:弹窗先放大到 1.05 倍,再回弹到 1.0 倍,形成轻微的"呼吸感",视觉上最柔和,适合大多数场景。
2️⃣ ScaleFade:缩放更极致的登场
TYAlertScaleFadeAnimation(TYAlertScaleFadeAnimation.m)从 0.1 倍直接放大到正常大小,0.3 秒一气呵成,节奏更快、更有"弹出来"的干脆感。
3️⃣ DropDown:从顶部掉落
TYAlertDropDownAnimation(TYAlertDropDownAnimation.m)让弹窗从屏幕顶部之上掉落下来,入场使用弹簧动画(damping 0.65),带一点自然的弹跳。
⚠️ 注意:DropDown 只支持 Alert 居中弹窗样式;若用于 ActionSheet 样式,源码会打印 don't support ActionSheet style!,动画将不生效。
三种切换动画的快捷写法
写法一:创建控制器时指定 transitionAnimation
最直观的方式——用带 transitionAnimation: 的工厂方法创建控制器(Demo 中 DropDown 示例见 ViewController.m):
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView
preferredStyle:TYAlertControllerStyleAlert
transitionAnimation:TYAlertTransitionAnimationDropDown];
[self presentViewController:alertController animated:YES completion:nil];
把枚举值换成 TYAlertTransitionAnimationScaleFade 即可切换为缩放淡入淡出。
写法二:用 UIView 分类一行弹出
UIView+TYAlertView 分类(UIView+TYAlertView.h)提供了带动画参数的便捷方法,任意视图都能直接弹:
[alertView showInController:self preferredStyle:TYAlertControllerStyleAlert transitionAnimation:TYAlertTransitionAnimationScaleFade];
不传动画参数时,分类内部默认使用 Fade(见 UIView+TYAlertView.m)。
写法三:自定义动画类(Custom)
内置三种不够用?可以传自定义动画类:
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView
preferredStyle:TYAlertControllerStyleAlert
transitionAnimationClass:MyAnimationClass];
此时控制器内部走 TYAlertTransitionAnimationCustom 分支,调用你的类来生成动画对象(见 TYAlertController+TransitionAnimate.m)。
如何编写一个自定义转场动画?
所有动画都继承基类 TYBaseAnimation(实现 UIViewControllerAnimatedTransitioning 协议),它已经帮你处理了 animateTransition: 的分发逻辑(TYBaseAnimation.m)。自定义时只需重写 3 个方法(见 TYBaseAnimation.h):
transitionDuration:—— 控制动画时长;presentAnimateTransition:—— 入场动画;dismissAnimateTransition:—— 出场动画。
基类还预留了一个小技巧(TYBaseAnimation.h):如果你的动画只适配某种样式,可以在 alertAnimationIsPresenting:preferredStyle: 中根据 preferredStyle 判断,不适配时返回 nil 即可回退到默认行为。
动画是怎么被分发的?
原理很简单:TYAlertController (TransitionAnimate) 作为 UIViewControllerTransitioningDelegate,在 present/dismiss 时根据 transitionAnimation 枚举 switch 出对应的动画类实例(TYAlertController+TransitionAnimate.m)。入场和出场分别取 isPresenting:YES / NO 两个实例,所以你可以针对入场、出场设计完全不同的效果——三种内置动画正是这么做的(例如 Fade 出场时 ActionSheet 会向下滑出屏幕)。
选择建议与小结
- 常规提示、表单弹窗 →
Fade(默认,柔和自然); - 需要更强的出现感、强调焦点 →
ScaleFade(干脆利落); - 页面顶部通知、下拉式提醒 →
DropDown(仅限 Alert 居中样式); - 品牌化定制动效 → 继承
TYBaseAnimation写自定义类,通过transitionAnimationClass:传入。
💡 小贴士:ActionSheet 样式下三种内置动画都有"从屏幕底部滑入/滑出"的专属表现,切换动画时记得两种样式都测试一遍。
核心源码集中在 TYAlertControllerDemo/TYAlertController/ 目录,配合 README.md 的用法示例,几行代码即可让弹窗动效升级。
