项目作者: inamiy

项目描述 :
An incremental linear constraint-solving algorithm (Auto Layout) in Swift.
高级语言: Swift
项目地址: git://github.com/inamiy/Cassowary.git
创建时间: 2017-09-15T00:12:50Z
项目社区:https://github.com/inamiy/Cassowary

开源协议:MIT License

下载


Cassowary

An incremental linear constraint-solving algorithm (Auto Layout) in Swift, originally from the paper:
Solving Linear Arithmetic Constraints for User Interface Applications (1997)

This repository consists of 3 frameworks:

  • Simplex: Simplex tableau and its common operations
  • Cassowary: Core constraint-solving algorithm using Simplex
  • CassowaryUI: UIKit/AppKit wrapper on top of Cassowary

How to use

CassowaryUI

  1. import Cassowary
  2. import CassowaryUI
  3. let rootSize = CGSize(width: 320, height: 480)
  4. var solver = CassowaryUI.Solver()
  5. try! solver.addConstraints(view1, view2) { v1, v2 in
  6. return [
  7. // `v1` has fixed size (4:3 aspect ratio)
  8. v1.width == rootSize.width - 40,
  9. v1.height == v1.width * 3 / 4,
  10. // `v2` has fixed origin.x & width (flexible in vertical)
  11. v2.width == v1.width,
  12. v1.centerX == rootFrame.width / 2,
  13. v2.centerX == v1.centerX,
  14. // equal spacing (vertical)
  15. v1.top == 40 ~ .high,
  16. v2.top - v1.bottom == v1.top ~ .high,
  17. rootSize.height - v2.bottom == v1.top ~ .high,
  18. ]
  19. }
  20. solver.applyLayout()

This will result:

  1. <UIView: 0x7f8f1ee018c0; frame = (0 0; 320 480); layer = <CALayer: 0x608000220800>>
  2. | <UIView: 0x7f8f21001010; frame = (20 40; 280 210); layer = <CALayer: 0x60c000220da0>>
  3. | <UIView: 0x7f8f1ec015c0; frame = (20 290; 280 150); layer = <CALayer: 0x60c000220840>>

Acknowledgments

References

License

MIT