IBAnimatable 3.0 迁移指南:从字符串枚举到强类型 Swift 枚举的全面升级

  • 移动开发
  • UI组件

【免费下载链接】IBAnimatable

Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

项目地址: https://gitcode.com/gh_mirrors/ib/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 Builder3.0 Interface Builder
.SlideInLeft.slide(way: .in, direction: .left)SlideInLeftslide(in,left)
.SlideInRight.slide(way: .in, direction: .right)SlideInRightslide(in,right)
.SlideInDown.slide(way: .in, direction: .down)SlideInDownslide(in,down)
.SlideInUp.slide(way: .in, direction: .up)SlideInUpslide(in,up)
.SlideOutLeft.slide(way: .out, direction: .left)SlideOutLeftslide(out,left)
.SlideOutRight.slide(way: .out, direction: .right)SlideOutRightslide(out,right)
.SlideOutDown.slide(way: .out, direction: .down)SlideOutDownslide(out,down)
.SlideOutUp.slide(way: .out, direction: .up)SlideOutUpslide(out,up)
.SqueezeInLeft.squeeze(way: .in, direction: .left)SqueezeInLeftsqueeze(in,left)
.SqueezeInRight.squeeze(way: .in, direction: .right)SqueezeInRightsqueeze(in,right)
.SqueezeInDown.squeeze(way: .in, direction: .down)SqueezeInDownsqueeze(in,down)
.SqueezeInUp.squeeze(way: .in, direction: .up)SqueezeInUpsqueeze(in,up)
.SqueezeOutLeft.squeeze(way: .out, direction: .left)SqueezeOutLeftsqueeze(out,left)
.SqueezeOutRight.squeeze(way: .out, direction: .right)SqueezeOutRightsqueeze(out,right)
.SqueezeOutDown.squeeze(way: .out, direction: .down)SqueezeOutDownsqueeze(out,down)
.SqueezeOutUp.squeeze(way: .out, direction: .up)SqueezeOutUpsqueeze(out,up)
.FadeInLeft.slideFade(way: .in, direction: .left)FadeInLeftslideFade(in,left)
.FadeInRight.slideFade(way: .in, direction: .right)FadeInRightslideFade(in,right)
.FadeInDown.slideFade(way: .in, direction: .down)FadeInDownslideFade(in,down)
.FadeInUp.slideFade(way: .in, direction: .up)FadeInUpslideFade(in,up)
.FadeOutLeft.slideFade(way: .out, direction: .left)FadeOutLeftslideFade(out,left)
.FadeOutRight.slideFade(way: .out, direction: .right)FadeOutRightslideFade(out,right)
.FadeOutDown.slideFade(way: .out, direction: .down)FadeOutDownslideFade(out,down)
.FadeOutUp.slideFade(way: .out, direction: .up)FadeOutUpslideFade(out,up)
.SqueezeFadeInLeft.squeezeFade(way: .in, direction: .left)SqueezeFadeInLeftsqueezeFade(in,left)
.SqueezeFadeInRight.squeezeFade(way: .in, direction: .right)SqueezeFadeInRightsqueezeFade(in,right)
.SqueezeFadeInDown.squeezeFade(way: .in, direction: .down)SqueezeFadeInDownsqueezeFade(in,down)
.SqueezeFadeInUp.squeezeFade(way: .in, direction: .up)SqueezeFadeInUpsqueezeFade(in,up)
.SqueezeFadeOutLeft.squeezeFade(way: .out, direction: .left)SqueezeFadeOutLeftsqueezeFade(out,left)
.SqueezeFadeOutRight.squeezeFade(way: .out, direction: .right)SqueezeFadeOutRightsqueezeFade(out,right)
.SqueezeFadeOutDown.squeezeFade(way: .out, direction: .down)SqueezeFadeOutDownsqueezeFade(out,down)
.SqueezeFadeOutUp.squeezeFade(way: .out, direction: .up)SqueezeFadeOutUpsqueezeFade(out,up)
.FadeIn.fade(way: .in)FadeInfade(in)
.FadeOut.fade(way: .out)FadeOutfade(out)
.FadeInOut.fade(way: .inOut)FadeInOutfade(inOut)
.FadeOutIn.fade(way: .outIn)FadeOutInfade(outIn)
.ZoomIn.zoom(way: .in)ZoomInzoom(in)
.ZoomOut.zoom(way: .out)ZoomOutzoom(out)
.ZoomInvertIn.zoomInvert(way: .in)ZoomInvertInzoomInvert(in)
.ZoomInvertOut.zoomInvert(way: .out)ZoomInvertOutzoomInvert(out)
.Shake.shake(repeatCount: 1)Shakeshake(1)
.Pop.pop(repeatCount: 1)Poppop(1)
.Squeeze.squash(repeatCount: 1)Squeezesquash(1)
.Morph.morph(repeatCount: 1)Morphmorph(1)
.Flash.flash(repeatCount: 1)Flashflash(1)
.Wobble.wobble(repeatCount: 1)Wobblewobble(1)
.Swing.swing(repeatCount: 1)Swingswing(1)
.FlipX.flip(along: .x)FlipXflip(x)
.FlipY.flip(along: .y)FlipYflip(y)
.Rotate.rotate(direction: .cw, repeatCount: 1)Rotaterotate(cw,1)
.Rotate.rotate(direction: .ccw, repeatCount: 1)Rotaterotate(ccw,1)
.MoveTo.moveTo(x: 100, y: 120)MoveTomoveTo(100,120)
.MoveBy.moveBy(x: -10, y: 20)MoveBymoveBy(-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 → configureAnimatableProperties
  • configMask → 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.

原因是属性名发生了变更。多数情况下,直接给旧属性名加一个下划线前缀(_)即可修复。有三种修复途径:

  1. 用你喜欢的编辑器(Atom、VS Code、Sublime 等)打开 *.storyboard 文件,按下列对照表批量替换。
  2. 在 Interface Builder 中打开 Storyboard,选中报错元素,打开 Identity inspector(标识检查器),修改 User Defined Runtime Attributes(用户自定义运行时属性)中的值。
  3. 在 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 迁移:

  1. 确认环境:iOS 8.0+、Xcode 8.0+、Swift 3.0+,否则停留 2.x。
  2. 代码层:运行 Xcode 的 "Convert to Current Swift Syntax...",再手动修正带参枚举(.slide(.in, direction: .left))、移除 x/y/repeatCount 独立属性、改用分组后的动画方法(slide(_:direction:) 等)、把 config*** 方法改名为 configure***。
  3. 自定义控件:为基于协议的枚举属性补齐 @IBInspectable var _xxx: String? 桥接属性,并在 didSet 中调用 XxxType(string:)。
  4. Storyboard:按对照表将旧属性名加下划线(如 animationType → _animationType),同时把属性值改为函数式字符串语法。
  5. 回归验证:构建工程确认无 “not key value coding-compliant” 类警告,并逐个验证动画、遮罩、转场、弹窗、手势等效果在 Attributes inspector 与运行时一致。

如果迁移过程中发现文档或库本身的问题,可以在仓库提交 Issue;有解决方案时欢迎提交 PR 回馈社区。相关背景可进一步阅读仓库内的 IBAnimatable 4.0 Migration Guide、Transitions.md 与 Presentations.md。

  • 移动开发
  • UI组件

【免费下载链接】IBAnimatable

Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

项目地址: https://gitcode.com/gh_mirrors/ib/IBAnimatable
点击查看 免费下载

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值