测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年5月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Adrian Zubarev 维护。
编写优美的约束
import FunctionalLayout
let constraint = Constraint.where(view1.centerXAnchor == view2.centerXAnchor)
.named("CenterXConstraint")
.withPriority(of: 800)
.shouldBeArchived()
.nsLayoutConstraint
constraint.isActive = true
self.constraint = Constraint.where(view1.centerYAnchor == view2.centerYAnchor)
.named("CenterYConstraint")
.activated()
.nsLayoutConstraint
NSLayoutConstraint.activate([
Constraint.where(view1.widthAnchor == 200).named("WidthConstraint"),
Constraint.where(view1.heightAnchor == view2.heightAnchor)
.multiplied(with: 2.0)
.offset(by: -50)
.named("HeightConstraint")
.withPriority(of: 1000)
.shouldBeArchived()
.assignedTo(&self.someConstraintVariable)
])
FunctionalLayout
通过 CocoaPods 提供。要安装,只需将以下行添加到 Podfile 中
pod 'FunctionalLayout'
此 API 使用协议以复杂的功能排列图(见图解)进行建模。每个字母代表对应协议中包含的函数。
红色节点代表四个静态函数的起点
Constraint.where(_: Layout<NSLayoutXAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutYAxisAnchor>) -> OWSN
Constraint.where(_: Layout<NSLayoutDimension>) -> OMWSN
Constraint.where(_: Layout<CGFloat>) -> WSN
可以通过三种不同的中缀运算符来构造 Layout<...>
实例。
func ==<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func >=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func <=<T>(lhs: NSLayoutAnchor<T>, rhs: NSLayoutAnchor<T>) -> Layout<T>
func ==(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func >=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
func <=(lhs: NSLayoutDimension, rhs: CGFloat) -> Layout<CGFloat>
此 API 对除 ActivationInterface
和 NSLayoutConstraintInterface
之外的所有类型保证值语义。[1] 对于所有保证的值语义类型,每个链在底层创建一个新的不可变值类型实例。
[1] 值语义:realm.io/news/swift-gallagher-value-semantics
FunctionalLayout
可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。