BasicAnimation
https://github.com/ZuopanYao/BasicAnimation
iOS 属性动画:缩放、旋转、平移、背景颜色、透明度、阴影等,一句代码的事
支持以下 KeyPath:
public enum BAKeyPath: String {
/// x、y轴缩放
case scale = "transform.scale"
/// x轴缩放
case scaleX = "transform.scale.x"
/// y轴缩放
case scaleY = "transform.scale.y"
/// z轴缩放
case scaleZ = "transform.scale.Z"
/// x旋转
case rotationX = "transform.rotation.x"
/// y轴旋转
case rotationY = "transform.rotation.y"
/// z轴旋转
case rotationZ = "transform.rotation.z"
/// x、y轴平面移动(CGSize)
case translation = "transform.translation"
/// x轴平面移动
case translationX = "transform.translation.x"
/// y轴平面移动
case translationY = "transform.translation.y"
/// z轴平面移动
case translationZ = "transform.translation.z"
/// CATransform3D
case transform = "transform"
/// 层级级别(值越大层级越高, 越接近顶层)
case zPosition = "zPosition"
/// 透明度
case opacity = "opacity"
/// 背景颜色
case backgroundColor = "backgroundColor"
/// 圆角半径
case cornerRadius = "cornerRadius"
/// 边框宽度
case borderWidth = "borderWidth"
///
case bounds = "bounds"
///
case contents = "contents"
case contentsRect = "contentsRect"
/// 隐藏
case hidden = "hidden"
/// 遮罩 (CALayer)
//case doubleSided = "doubleSided"
/// 是否将子层剪裁到层的边界
//case masksToBounds = "masksToBounds"
/// CALayer在父层中的位置
case position = "position"
/// 阴影颜色
case shadowColor = "shadowColor"
/// 阴影偏移
case shadowOffset = "shadowOffset"
/// 阴影透明度
case shadowOpacity = "shadowOpacity"
/// 阴影半径
case shadowRadius = "shadowRadius"
}
Requirements / 使用条件
- iOS 10.0+
- Xcode 12.2+
- Swift 5.0+
Installation / 安装
CocoaPods
pod 'BasicAnimation'
Manually / 手动安装
If you prefer not to use either of the aforementioned dependency managers, you can integrate BasicAnimation into your project manually.
如果您不喜欢以上管理依赖库的方式,则可以手动将 BasicAnimation 集成到项目中。
Usage / 使用
Quick Start / 快速上手
Example 1 / 示例 1
import BasicAnimation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let aView = UIView(frame: CGRect(x: 100, y: 80, width: 180, height: 80))
aView.backgroundColor = UIColor.red
view.addSubview(aView)
/// 缩放动画
/// scale Animation
let animation = BAAnimation.scale(from: CGPoint(x: 1.0, y: 1.0), to: CGPoint(x: 0.4, y: 0.5))
aView.ba.create(animation: animation).delay(2.0).run()
}
}
Example 2 / 示例 2
import BasicAnimation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let bView = UIView(frame: CGRect(x: 100, y: 230, width: 120, height: 120))
bView.backgroundColor = UIColor.blue
view.addSubview(bView)
/// 旋转动画
/// rotation Animation
bView.ba.create(animation: .rotationZ(from: 0, to: Float.pi * 2.0)).duration(1.0).repeatCount(999).timingCurve(.linear).run()
}
}
Example 3: animation group / 示例 3
import BasicAnimation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cView = UIView(frame: CGRect(x: 130, y: 400, width: 120, height: 120))
cView.backgroundColor = UIColor.purple
cView.layer.shadowOffset = .zero
view.addSubview(cView)
let animation1 = BAAnimation.shadowColor(from: UIColor.black.cgColor, to: UIColor.green.cgColor).create()
let animation2 = BAAnimation.shadowRadius(from: 0.0, to: 50.0).create()
let animation3 = BAAnimation.shadowOpacity(from: 0.2, to: 0.8).create()
/// animation group 动画组
cView.ba.create(animation: [animation1, animation2, animation3]).delay(3.0).duration(2.0).repeatCount(9999).isReverses(true).run()
}
}
Example 4: animation group / 示例 4
import BasicAnimation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let dView = UIView(frame: CGRect(x: 0, y: 00, width: 120, height: 120))
dView.backgroundColor = UIColor.yellow
view.addSubview(dView)
///
let animation4 = BAAnimation.rotationZ(from: 0, to: Float.pi * 2.0).create()
let animation5 = BAAnimation.position(from: dView.center, to: CGPoint(x: 300, y: 400)).create()
let groupAnimation = dView.ba.create(animation: [animation4, animation5])
groupAnimation.delay(3.0)
.duration(2.0)
.timingCurve(.easeInEaseOut)
.repeatCount(9999)
.isReverses(true)
.run()
/// Maybe
// dView.ba.removeAnimation(animation: groupAnimation)
}
}
Spring Animation
需要位置发生变化,弹性动画才起作用
/// Simple
let animation = BAAnimation.position(from: myView.center, to:CGPoint(x: 200, y: 200))
myView.ba.create(animation: animation, spring: .default).run()
/// custom
let mySpring = BASpring(10.0, mass: 2.0, stiffness: 100.0, damping: 5.0)
let animation = BAAnimation.position(from: myView.center, to:CGPoint(x: 200, y: 200))
myView.ba.create(animation: animation, spring: .mySpring).run()