【免费下载链接】TYAlertController
Powerful, Easy to use alert view or popup view on controller and window, support blur effects,custom view and animation,for objective-c,support iphone, ipad
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 的用法示例,几行代码即可让弹窗动效升级。
【免费下载链接】TYAlertController
Powerful, Easy to use alert view or popup view on controller and window, support blur effects,custom view and animation,for objective-c,support iphone, ipad
相关推荐
Shimmer-android终极指南:7种动态动画效果切换技巧
Shimmer android终极指南:7种动态动画效果切换技巧 想要为你的Android应用添加令人惊艳的动态闪烁效果吗?Shimmer android是Fa
UI组件移动开发前端YCBlogs 补间动画实战详解:PopupWindow、Activity 转场与 ViewGroup 进场动画三大应用场景
YCBlogs 补间动画实战详解:PopupWindow、Activity 转场与 ViewGroup 进场动画三大应用场景 补间动画(Tween Animat
教程技术博客文档YCBlogs 动画实践:Android 转场动画(Transition)基础详解——场景、变换与共享元素
YCBlogs 动画实践:Android 转场动画(Transition)基础详解——场景、变换与共享元素 本篇技术指南围绕 Android 5.0 引入的 T
教程技术博客文档
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考