SwiftyAnimate 1.3.0

SwiftyAnimate 1.3.0

测试已测试
Lang语言 SwiftSwift
许可 MIT
Released最新发布2017年2月
SwiftSwift 版本3.0
SPM支持 SPM

Reid Chatham 维护。



  • 作者:
  • Reid Chatham

Swift中的组合动画。 博客

Code Climate

安装

Swift包管理器

将以下行添加到您的Package.swift文件中。

.Package(url: "https://github.com/rchatham/SwiftyAnimate.git", majorVersion: 0) 

用法

此库可用于设计可组合的动画,同时保持动画代码可读性和可维护性。

组合动画

使用thendowait函数组合动画并在它们之间插入逻辑。

Then 块

使用该函数的某个实现将动画添加到当前实例。该函数有用于弹簧和关键帧动画的实现,以及将Animate对象连在一起。

Animate(duration: 1.0) {
        // animation code goes here
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

Do 块

在此处添加您不打算进行动画处理的代码,但希望在动画之间执行这些代码。您放入这里的任何代码都不会进行动画处理。

Animate(duration: 1.0) {
        // animation code goes here
    }
    .do {
        // logic you don't want to animate
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

Wait 块

添加可能需要暂停进行中的动画链的代码。您放入这里的任何代码都不会进行动画处理。如果您想等待特定的时间,或者不想无限期地等待执行Wait块中的代码,可以传递一个超时时间。

Animate(duration: 1.0) {
        // animation code goes here
    }
    .wait(timeout: 5.0) { resume in
        // logic you want to pause an animation to complete
        resume()
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

执行动画

有两种方式来执行动画:finishperform。**重要提示:您必须调用这两个函数之一或对动画实例调用**decay,否则会导致内存泄漏!

Perform

这个很简单。在动画实例上调用它来执行。Perform 接受一个可选的闭包,它将在完成最后一个动画块时执行。

let animation = Animate(duration: 1.0) {
    // animations
}

animation.perform()

完成

如果你不需要传递一个完成闭包,尝试在动画实例上调用 finish。传入的动画将排队,然后对实例调用 perform。Finish 与 then 函数具有所有相同的变体。

Animate(duration: 1.0) {
        // animations
    }
    .finish(duration: 1.0) {
        // animations
    }

衰减

如果你想在不执行动画的情况下释放动画实例,请在上面调用衰减。

let animation = Animate(duration: 1.0) {
    // animations
}

animation.decay()

UIView 扩展

许多可动画属性都有了扩展,使得使用这个库实现它们变得更加容易。请查看文档!

最佳实践

充分利用这个库的最佳方式是为你想进行动画的视图定义扩展。这可以让你更好地将代码分类,并将所有动画逻辑都封装在视图内部,避免它们出现在视图控制器中。

extension AnimatingView {
    func bounceAnimation() -> Animate {
        return Animate()
            .then(animation: scale(duration: 0.3, x: 1.3, y: 1.3))
            .then(animation: scale(duration: 0.3, x: 0.8, y: 0.8))
            .then(animation: scale(duration: 0.3, x: 1.1, y: 1.1))
            .then(animation: scale(duration: 0.3, x: 1.0, y: 1.0))
    }
}

然后在你想要执行动画时,只需对返回的动画调用 perform() 即可。

let animatingView = AnimatingView()

animatingView.bounceAnimation().perform()

将它们与其他动画串联起来,可以轻松构建复杂的动画逻辑。

Animate()
    .then(animation: animatingView.bounceAnimation())
    .then(animation: animatingView.tiltAnimation())
    .then(animation: animatingView.bounceAnimation())
    .perform()

贡献

我非常希望看到你改进这个库的想法!最好的贡献方式是提交拉取请求。我会尽最大努力尽快回应你的补丁。如果你发现错误或有疑问,你还可以提交一个新的 GitHub 问题🙏

请确保遵循我们的通用编码风格,并为新功能添加测试覆盖率!