AZTransitions 0.26.0

AZTransitions 0.26.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新版本2021年2月
SPM支持 SPM

Alex Zimin维护。



  • Alexander Zimin

AZTransitions

CocoaPods Compatible Carthage Compatible SPM Compatible Platform Twitter GitHub license

使用自定义动画制作 modal 转换。AZTransitions 通过特定的 API 方法帮助您发挥创意。

视觉示例

在这个仓库中,您可以尝试 iOS Example 目标,其中包含示例 FashionTransition.swift 类。

Animation example

安装

  • 将以下内容添加到您的 Podfile 并运行 pod install
pod 'AZTransitions'
  • 或者将以下内容添加到您的 Cartfile 并运行 carthage update
github "azimin/AZTransitions"
  • 如果您正在使用 Swift Package Manager,只需将其添加到 Package.swiftdependencies 值中
dependencies: [
    .package(url: "https://github.com/azimin/AZTransitions.git", .upToNextMajor(from: "0.26.0"))
]
  • 或作为 git 子模块克隆,

  • 或者只需将 AZTransitions/Source/CustomModalTransition.swift 复制到您的项目中。

代码示例

要创建任何自定义转换,只需继承CustomModalTransition

class FashionTransition: CustomModalTransition { 
  override init() {
    super.init(duration: 0.5)
  }
}

--

然后在展示之前将其设置为az_modalTransition所需的视图

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  segue.destination.customModalTransition = FashionTransition()
}

或者

func show() {
  let viewController = UIViewController()
  viewController.customModalTransition = FashionTransition()
  self.present(viewController, animated: true, completion: nil)
}

--

要自定义展示动画,只需在您的类中实现performTransition(interactive: Bool)

func performTransition(interactive: Bool) {
  self.presentedViewController.view.alpha = 0.0
    
  UIView.animate(withDuration: duration, animations: {
    self.presentedViewController.view.alpha = 1.0
    self.presentingViewController.view.alpha = 0.0
  }, completion: { (completed) in
    self.presentingViewController.view.alpha = 1.0
    self.finishAnimation(completion: nil)
  })
}

正如您所猜想的,您有不同的属性。主要的包括

  • duration — 转换持续时间
  • presentingViewController — 展示视图控制器(下方的控制器)
  • presentedViewController — 要展示的视图控制器(上方的控制器)

您可以按需对它们进行动画处理。

🔥重要!🔥请勿忘记在最后调用finishAnimation(completion: nil)

在这种情况下动画将是

Animation code example

UIModalPresentationStyle

当然,有时您想要使用不同的模态展示风格(例如overCurrentContext),在这种情况下,您可以对UIViewController调用setCustomModalTransition(customModalTransition: CustomModalTransition, inPresentationStyle: UIModalPresentationStyle),而不是直接设置customModalTransition

更多内容

您有多种属性和方法来帮助您

  • performDismissingTransition(interactive: Bool)来在消失时实现自定义转换动画
  • fromViewController/toViewController从Apple转换的角度来看。它们在展示和消失转换时是相反的。
  • transitionContainerView转换发生的视图(resentingViewController.view和在transitionContainerView内部定位的presentedViewController.view),因此您可以在其中添加自定义视图以使动画更加有趣(请参阅iOS 示例
  • 一些交互式动画的方法(示例将很快添加)
  • 一些处理方向更改的方法(示例将很快添加)