【免费下载链接】IBAnimatable
Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.
IBAnimatable 是一款通过 Interface Builder 设计、原型化自定义 UI、交互、导航、转场与动画的 iOS 开源库。本文基于仓库中的官方迁移文档,系统讲解从 2.x 升级到 3.0 时遇到的 API 破坏性变更(Breaking Changes)——核心是从“字符串拼接式(stringly typed)”API 全面切换到基于 Swift 枚举的强类型(strongly typed)API。读完本文,你将掌握 AnimationType 等枚举的新写法、Interface Builder 中 slide(in,left) 式属性值语法、旧属性名加下划线的修复方法,以及自定义控件如何适配新协议,从而顺利迁移存量工程。
升级前提与收益
版本与系统要求
3.0 作为遵循语义化版本(Semantic Versioning)的主版本发布,引入了一系列 API 破坏性变更。升级前请确认工程满足以下要求:
- iOS 8.0+
- Xcode 8.0+
- Swift 3.0+
如果项目仍停留在 Swift 2.2 或 2.3,请继续使用支持这两个版本的最新 2.x tagged 版本,暂不升级。
升级的两大收益
- 完整的 Swift 3 兼容性:全面遵循 Swift 官方 API 设计规范(API Design Guidelines),所有 API 命名与参数风格均按新规范重写。
- 全新的枚举体系:用强类型枚举替换原来以字符串为载体的“字符串型”API,在编译期即可获得类型检查与自动补全,避免手写字符串带来的拼写错误。
破坏性变更总览
由于 3.0 全面采用 Swift 3 的新语法与命名约定,几乎每一个公开 API 都发生了变化。文档无法逐一列举全部改动,因此下面聚焦最常见、最可能引发编译器报错的 API 变更,并给出迁移前后对照。
枚举:全部改为小写开头
所有枚举 case 从大写驼峰改为小写开头(Swift 3 命名规范)。例如 .SlideInLeft 变为 .slide(...)。在 Xcode 中运行 "Convert to Current Swift Syntax..."(转换为当前 Swift 语法)菜单即可自动完成大部分这类重命名。
枚举的底层设计:从字符串到带参枚举
这是 3.0 最重要的架构变化。由于 @IBInspectable 属性无法直接支持 Swift 枚举,2.x 中不得不把 AnimationType 这类枚举在协议里声明成 String:
public enum AnimationType {
case SlideInLeft
case SlideInRight
// ...
}
// 协议中的属性只能是 String
var animationType: String? { get set }
这样做的代价是:代码里必须用字符串字面量赋值,比如 view.animationType = "SlideInLeft",既无类型安全,也容易拼错。
3.0 引入了一个“两全其美”的解决方案:枚举 case 支持携带参数(associated values),同时在 @IBInspectable 侧用 String 桥接(见下文 IBEnum 桥接机制)。新的 AnimationType 定义如下:
public enum AnimationType {
case slide(way: Way, direction: Direction)
case fade(way: FadeWay)
case shake(repeatCount: Int)
// ...
}
代码中使用方式:
var view = AnimatableView() // 从 Storyboard 或 frame 创建视图
view.animationType = .slide(.in, direction: .left)
// view.animationType = .slide(.out, direction: .right)
而在 Attributes inspector(属性检查器)中,Animation Type 的输入值变为函数式语法:
slide(in,left)slide(out,right)
AnimationType 迁移对照表
下表完整列出常用动画从 2.x(代码字符串 / IB 字符串)到 3.0(枚举 / IB 函数式字符串)的对应关系:
| 2.x 代码中的字符串值 | 3.0 代码中的枚举值 | 2.x Interface Builder | 3.0 Interface Builder |
|---|---|---|---|
| .SlideInLeft | .slide(way: .in, direction: .left) | SlideInLeft | slide(in,left) |
| .SlideInRight | .slide(way: .in, direction: .right) | SlideInRight | slide(in,right) |
| .SlideInDown | .slide(way: .in, direction: .down) | SlideInDown | slide(in,down) |
| .SlideInUp | .slide(way: .in, direction: .up) | SlideInUp | slide(in,up) |
| .SlideOutLeft | .slide(way: .out, direction: .left) | SlideOutLeft | slide(out,left) |
| .SlideOutRight | .slide(way: .out, direction: .right) | SlideOutRight | slide(out,right) |
| .SlideOutDown | .slide(way: .out, direction: .down) | SlideOutDown | slide(out,down) |
| .SlideOutUp | .slide(way: .out, direction: .up) | SlideOutUp | slide(out,up) |
| .SqueezeInLeft | .squeeze(way: .in, direction: .left) | SqueezeInLeft | squeeze(in,left) |
| .SqueezeInRight | .squeeze(way: .in, direction: .right) | SqueezeInRight | squeeze(in,right) |
| .SqueezeInDown | .squeeze(way: .in, direction: .down) | SqueezeInDown | squeeze(in,down) |
| .SqueezeInUp | .squeeze(way: .in, direction: .up) | SqueezeInUp | squeeze(in,up) |
| .SqueezeOutLeft | .squeeze(way: .out, direction: .left) | SqueezeOutLeft | squeeze(out,left) |
| .SqueezeOutRight | .squeeze(way: .out, direction: .right) | SqueezeOutRight | squeeze(out,right) |
| .SqueezeOutDown | .squeeze(way: .out, direction: .down) | SqueezeOutDown | squeeze(out,down) |
| .SqueezeOutUp | .squeeze(way: .out, direction: .up) | SqueezeOutUp | squeeze(out,up) |
| .FadeInLeft | .slideFade(way: .in, direction: .left) | FadeInLeft | slideFade(in,left) |
| .FadeInRight | .slideFade(way: .in, direction: .right) | FadeInRight | slideFade(in,right) |
| .FadeInDown | .slideFade(way: .in, direction: .down) | FadeInDown | slideFade(in,down) |
| .FadeInUp | .slideFade(way: .in, direction: .up) | FadeInUp | slideFade(in,up) |
| .FadeOutLeft | .slideFade(way: .out, direction: .left) | FadeOutLeft | slideFade(out,left) |
| .FadeOutRight | .slideFade(way: .out, direction: .right) | FadeOutRight | slideFade(out,right) |
| .FadeOutDown | .slideFade(way: .out, direction: .down) | FadeOutDown | slideFade(out,down) |
| .FadeOutUp | .slideFade(way: .out, direction: .up) | FadeOutUp | slideFade(out,up) |
| .SqueezeFadeInLeft | .squeezeFade(way: .in, direction: .left) | SqueezeFadeInLeft | squeezeFade(in,left) |
| .SqueezeFadeInRight | .squeezeFade(way: .in, direction: .right) | SqueezeFadeInRight | squeezeFade(in,right) |
| .SqueezeFadeInDown | .squeezeFade(way: .in, direction: .down) | SqueezeFadeInDown | squeezeFade(in,down) |
| .SqueezeFadeInUp | .squeezeFade(way: .in, direction: .up) | SqueezeFadeInUp | squeezeFade(in,up) |
| .SqueezeFadeOutLeft | .squeezeFade(way: .out, direction: .left) | SqueezeFadeOutLeft | squeezeFade(out,left) |
| .SqueezeFadeOutRight | .squeezeFade(way: .out, direction: .right) | SqueezeFadeOutRight | squeezeFade(out,right) |
| .SqueezeFadeOutDown | .squeezeFade(way: .out, direction: .down) | SqueezeFadeOutDown | squeezeFade(out,down) |
| .SqueezeFadeOutUp | .squeezeFade(way: .out, direction: .up) | SqueezeFadeOutUp | squeezeFade(out,up) |
| .FadeIn | .fade(way: .in) | FadeIn | fade(in) |
| .FadeOut | .fade(way: .out) | FadeOut | fade(out) |
| .FadeInOut | .fade(way: .inOut) | FadeInOut | fade(inOut) |
| .FadeOutIn | .fade(way: .outIn) | FadeOutIn | fade(outIn) |
| .ZoomIn | .zoom(way: .in) | ZoomIn | zoom(in) |
| .ZoomOut | .zoom(way: .out) | ZoomOut | zoom(out) |
| .ZoomInvertIn | .zoomInvert(way: .in) | ZoomInvertIn | zoomInvert(in) |
| .ZoomInvertOut | .zoomInvert(way: .out) | ZoomInvertOut | zoomInvert(out) |
| .Shake | .shake(repeatCount: 1) | Shake | shake(1) |
| .Pop | .pop(repeatCount: 1) | Pop | pop(1) |
| .Squeeze | .squash(repeatCount: 1) | Squeeze | squash(1) |
| .Morph | .morph(repeatCount: 1) | Morph | morph(1) |
| .Flash | .flash(repeatCount: 1) | Flash | flash(1) |
| .Wobble | .wobble(repeatCount: 1) | Wobble | wobble(1) |
| .Swing | .swing(repeatCount: 1) | Swing | swing(1) |
| .FlipX | .flip(along: .x) | FlipX | flip(x) |
| .FlipY | .flip(along: .y) | FlipY | flip(y) |
| .Rotate | .rotate(direction: .cw, repeatCount: 1) | Rotate | rotate(cw,1) |
| .Rotate | .rotate(direction: .ccw, repeatCount: 1) | Rotate | rotate(ccw,1) |
| .MoveTo | .moveTo(x: 100, y: 120) | MoveTo | moveTo(100,120) |
| .MoveBy | .moveBy(x: -10, y: 20) | MoveBy | moveBy(-10,20) |
注:原文档迁移表中存在少量笔误(如
.FadeOutLeft对应方向重复),上表按.FadeOut系列的实际语义补充了Right/Down/Up行,迁移时以 3.x 源码 AnimationType.swift 中的实际定义为准。
其余枚举的迁移
BlurEffectStyle:已被系统枚举UIBlurEffectStyle直接替代,不再需要自定义枚举。MaskType:代码中使用:
var maskType: MaskType = .circle
maskType = .triangle
maskType = .star(points: 6)
maskType = .polygon(sides: 10)
maskType = .parallelogram(angle: 60)
maskType = .wave(direction: .up, width: 50, offset: 10)
Interface Builder 中对应写:circle、star(6)、polygon(10)、parallelogram(60) 或 wave(up,50,10)。
TransitionAnimationType:代码中使用:
var transitionAnimationType: TransitionAnimationType = .systemRotate
transitionAnimationType = .cards(direction: .forward)
transitionAnimationType = .fade(direction: .in)
transitionAnimationType = .explode(xFactor: 10, minAngle: 10, maxAngle: 20)
transitionAnimationType = .systemCube(from: .left)
Interface Builder 中对应写:systemRotate、cards(forward)、fade(in)、explode(10,10,20) 或 systemCube(left)。
InteractiveGestureType:代码中使用:
var interactiveGestureType: InteractiveGestureType = .pan(from: .right)
interactiveGestureType = .pinch(direction: .open)
interactiveGestureType = .screenEdgePan(from: .left)
Interface Builder 中对应写:pan(right)、pinch(open) 或 screenEdgePan(left)。
PresentationAnimationType:代码中使用:
var presentationAnimationType: PresentationAnimationType = .flip
presentationAnimationType = .dropDown
presentationAnimationType = .crossDissolve
presentationAnimationType = .zoom
presentationAnimationType = .cover(from: .left)
Interface Builder 中对应写:flip、dropDown、crossDissolve、zoom 或 cover(left)。
PresentationModalSize:代码中使用:
var presentationModalSize: PresentationModalSize = .full
presentationModalSize = .half
presentationModalSize = .custom(size: 200)
Interface Builder 中对应写:full、half 或 custom(200)。
PresentationModalPosition:代码中使用:
var presentationModalPosition: PresentationModalPosition = .center
presentationModalPosition = .leftCenter
presentationModalPosition = .bottomCenter
presentationModalPosition = .customCenter(centerPoint: 220)
presentationModalPosition = .customOrigin(origin: 100)
Interface Builder 中对应写:center、leftCenter、bottomCenter、customCenter(220) 或 customOrigin(100)。
- 其他枚举(如
ActivityIndicatorType、ColorType、GradientType等):只需将所有 case 改为小写开头即可。
IBEnum 桥接机制:String 与 Enum 互转
上述“代码用枚举、IB 用字符串”的双轨制,靠 IBEnum 协议实现。该协议提供 init?(string: String?),把 @IBInspectable 的字符串解析成对应枚举(见 IBEnum.swift)。解析流程由 Udra.swift 中的词法/语法解析器完成:它把 slide(in,left) 这类字符串切分为 token、解析为调用节点(CallNode)与参数节点,再交给各枚举的 init(string:) 按名称与参数位置构建枚举值。因此 IB 中的字符串书写顺序必须与枚举参数顺序严格一致(如 wave(up,50,10) 对应 direction、width、offset),参数缺省时则使用源码中的默认值(如 polygon 默认 6 条边、wave 默认 width 40/offset 0,见 MaskType.swift)。另外,IB 中的字符串不区分大小写——解析器在分词前会把输入统一 lowercased()。
协议层变更
协议扩展方法统一更名
所有以 config*** 开头的协议扩展方法统一改为 configure***:
configAnimatableProperties→configureAnimatablePropertiesconfigMask→configureMask
Animatable 协议的属性调整
- 移除
x、y属性:二者成为moveTo(x:y:)与moveBy(x:y:)枚举的关联参数。
// 2.x 写法
view.x = 200
view.y = 300
view.animationType = "MoveTo"
view.animate()
// 3.0 写法
view.animationType = .moveTo(x: 200, y: 300)
view.animate()
- 移除
repeatCount属性:它成为shake、pop、squash、morph、flash、wobble、swing、rotate等枚举的关联参数。
// 2.x 写法
view.repeatCount = 5
view.animationType = "Pop"
view.animate()
// 3.0 写法
view.animationType = .pop(repeatCount: 1)
view.animate()
- 动画方法分组:随着
AnimationType枚举重构,类似动画方法被合并为单一方法。例如slideInLeft()、slideInRight()、slideInDown()、slideInUp()、slideOutLeft()、slideOutRight()、slideOutDown()、slideOutUp()八个方法统一收敛为slide(_ way: AnimationType.Way, direction: AnimationType.Direction):
// 2.x 写法
view.slideInLeft()
view.slideOutRight()
// 3.0 写法
view.slide(.in, direction: .left)
view.slide(.out, direction: .right)
其它动画方法采用同一分组模式。这一重构在源码 Animatable.swift 的 doAnimation 分发中体现得非常直观:每个枚举 case 一一对应一个底层动画实现(slide、squeeze、fade、zoom、shake、pop、rotate 等),且支持 .compound 组合动画按 .sequential/.parallel 顺序或并行执行。
带枚举协议的属性类型变更
所有协议的枚举属性从 String 改为真正的枚举类型。例如 Animatable 的 var animationType: String? { get set } 变为 var animationType: AnimationType { get set };MaskDesignable 的 var maskType: String? { get set } 变为 var maskType: MaskType { get set }。迁移后代码写法:
// 2.x 写法
view.animationType = "Pop"
view.maskType = "Circle"
// 3.0 写法
view.animationType = .pop(repeatCount: 2)
view.maskType = .circle
Animatable UI 元素与自定义控件的迁移
内置 Animatable 元素
IBAnimatable 采用协议导向编程(Protocol Oriented Programming),所有可动画 UI 元素(如 AnimatableView、AnimatableButton)复用同一套协议。协议切到新枚举体系后,这些元素的属性也随之改为枚举类型:
// 2.x 写法
view.animationType = "Pop"
view.maskType = "Circle"
button.animationType = "Shake"
// 3.0 写法
view.animationType = .pop(repeatCount: 2)
view.maskType = .circle
button.animationType = .shake(repeatCount: 1)
以 AnimatableView.swift 为例,可以看到 3.x 的真实实现:公开属性 maskType、animationType 是枚举,而 _maskType、_animationType 是以 @IBInspectable var 声明的 String?,通过 didSet 调用 MaskType(string:) / AnimationType(string:) 完成 IB 字符串到枚举的转换。
自定义 UI 元素(基于协议扩展)
如果你基于 IBAnimatable 协议实现了自己的自定义控件(例如 IBAnimatableMaterial 这类衍生项目),需要同步修改 animationType、maskType 等属性的实现。以 maskType 为例:
class CustomView: UIView, MaskDesignable {
// MARK: - MaskDesignable
open var maskType: MaskType = .none {
didSet {
configureMask()
configureBorder()
configureMaskShadow()
}
}
/// 供 Interface Builder 使用的 mask 类型。**不应**在代码中使用此属性。
@IBInspectable var _maskType: String? {
didSet {
maskType = MaskType(string: _maskType)
}
}
}
由于 Interface Builder 无法直接支持 Swift 枚举,必须额外声明一个 internal 的 String 属性 _maskType(仅用于 IB),在 didSet 中把字符串解析为真正的枚举 maskType。这套“公开枚举 + 内部字符串桥接属性”的模式,正是 3.0 在协议层的标准做法,MaskDesignable 的文档注释里也有明确说明(见 MaskDesignable.swift)。
Storyboard 与 Interface Builder 的修复
由于枚举体系重构,animationType、maskType 等属性在 Storyboard 中也需要一并修改。迁移后打开工程,通常会看到类似下面的警告:
DemoApp.storyboard: warning: IB Designables: Ignoring user defined runtime attribute for key path "animationType" on instance of "IBAnimatable.AnimatableButton". Hit an exception when attempting to set its value: [<IBAnimatable.AnimatableButton 0x7ffc3a974800> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key animationType.
原因是属性名发生了变更。多数情况下,直接给旧属性名加一个下划线前缀(_)即可修复。有三种修复途径:
- 用你喜欢的编辑器(Atom、VS Code、Sublime 等)打开
*.storyboard文件,按下列对照表批量替换。 - 在 Interface Builder 中打开 Storyboard,选中报错元素,打开 Identity inspector(标识检查器),修改 User Defined Runtime Attributes(用户自定义运行时属性)中的值。
- 在 Xcode 中搜索旧属性名定位(也可以“替换全部”,但可能比较危险)。
完整对照表如下:
| 变更前 | 变更后 |
|---|---|
| borderSides | _borderSides |
| blurEffectStyle | _blurEffectStyle |
| vibrancyEffectStyle | _vibrancyEffectStyle |
| predefinedGradient | _predefinedGradient |
| startPoint | _startPoint |
| maskType | _maskType |
| animationType | _animationType |
| predefinedColor | _predefinedColor |
| transitionAnimationType | _transitionAnimationType |
| interactiveGestureType | _interactiveGestureType |
| presentationAnimationType | _presentationAnimationType |
| dismissalAnimationType | _dismissalAnimationType |
| modalPosition | _modalPosition |
| modalWidth | _modalWidth |
| modalHeight | _modalHeight |
| keyboardTranslation | _keyboardTranslation |
加下划线后的属性在源码中确实存在:例如 AnimatableView.swift 中的
_animationType、_maskType、_predefinedColor、_blurEffectStyle、_startPoint、_predefinedGradient,以及 AnimatableButton.swift 等各视图文件中的_borderSides、_transitionAnimationType、_interactiveGestureType等,均遵循“公开枚举 + 内部String桥接属性”的同一模式。
另外注意:加下划线后的属性值内容本身也要符合 3.0 的“函数式字符串”语法(如 slide(in,left)、star(6)),而非旧的 SlideInLeft、Circle,否则解析失败会回退到 .none(对应代码中的 AnimationType.none / MaskType.none,即不产生任何效果)。
迁移清单速查
按以下顺序检查你的工程,即可系统完成 3.0 迁移:
- 确认环境:iOS 8.0+、Xcode 8.0+、Swift 3.0+,否则停留 2.x。
- 代码层:运行 Xcode 的 "Convert to Current Swift Syntax...",再手动修正带参枚举(
.slide(.in, direction: .left))、移除x/y/repeatCount独立属性、改用分组后的动画方法(slide(_:direction:)等)、把config***方法改名为configure***。 - 自定义控件:为基于协议的枚举属性补齐
@IBInspectable var _xxx: String?桥接属性,并在didSet中调用XxxType(string:)。 - Storyboard:按对照表将旧属性名加下划线(如
animationType→_animationType),同时把属性值改为函数式字符串语法。 - 回归验证:构建工程确认无 “not key value coding-compliant” 类警告,并逐个验证动画、遮罩、转场、弹窗、手势等效果在 Attributes inspector 与运行时一致。
如果迁移过程中发现文档或库本身的问题,可以在仓库提交 Issue;有解决方案时欢迎提交 PR 回馈社区。相关背景可进一步阅读仓库内的 IBAnimatable 4.0 Migration Guide、Transitions.md 与 Presentations.md。
【免费下载链接】IBAnimatable
Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



