PureLayout###
siwft
声明打造,怎么说也得谢谢UI试试手感,那么原先OC
布局库的支持还有么?
细细的去github搜寻了一番,
masonary
的升级版本snapKit
和继续支持swift
的pureLayout
什么?居然还是继续支持swift中的自动布局?那赶紧用起来呗(一直觉得这个库布局更顺手,使用起来也是要有全都有啊,据说作者进了apple?)
- [x] 首先,自然是要导入这个三方库
pod search PureLayout
瞅瞅
PureLayout (3.0.1)
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful.
Objective-C and Swift compatible.
pod 'PureLayout', '~> 3.0.1'
- Homepage: https://github.com/PureLayout/PureLayout
- Source: https://github.com/PureLayout/PureLayout.git
- Versions: 3.0.1, 3.0.0, 2.0.6, 2.0.5, 2.0.4, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 1.1.0, 1.0.1, 1.0.0
既然是swift,Podfile中这样了
pod 'PureLayout', '~> 3.0.1'
use_frameworks!
这个原因是因为,swift
中新增加了cocoaframework
这个概念,所以和原来关联的静态库有所不同
- [x] 导入库之后,自然就打开
workSpace
就是了.swift
中新增了命名空间这样的概念,所以使用cocoaPods
可以避免不少冲突(类之间的冲突)
而且swift中,一次导入,到处使用啊,烦人的头文件不见了~
import PureLayout
import UIKit
class CodeView: UIView {
override init(frame: CGRect) {
super.init(frame:frame)
addSubview(containerView)
containerView.addSubview(smashView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private lazy var containerView:UIImageView = {
let container = UIImageView(image: UIImage(named: "qrcode_border"))
return container
}()
private lazy var smashView:UIImageView = {
let smash = UIImageView(image: UIImage(named: "qrcode_scanline_qrcode"))
return smash
}()
override func layoutSubviews() {
super.layoutSubviews()
containerView.autoPinEdgesToSuperviewEdges()//充满
smashView.autoPinEdgesToSuperviewEdges()//充满
}
}
- [x] 布局是最常见的使用,通过约束来做动画有时也是无法避免的,我是重度纯代码使用者
// 将要改动的约束保存起来
var codeViewHeightConstrains:NSLayoutConstraint?
var scanViewTopContrains:NSLayoutConstraint?
//设置约束
private func setUpCodeView(){
view.addSubview(codeView)
codeView.autoCenterInSuperview()
codeViewHeightConstrains = codeView.autoSetDimension(ALDimension.Height, toSize: 180)
codeView.autoSetDimension(ALDimension.Width, toSize: 180)
codeView.addSubview(scanView)
scanView.autoPinEdgeToSuperviewEdge(ALEdge.Leading)
scanView.autoPinEdgeToSuperviewEdge(ALEdge.Trailing)
scanView.autoSetDimension(ALDimension.Height, toSize: (codeViewHeightConstrains?.constant)!)
scanViewTopContrains = scanView.autoPinEdge(ALEdge.Top, toEdge: ALEdge.Top, ofView: codeView, withOffset: 0)
}
//然后就可以在viewWillAppear中做动画去了
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
scanViewTopContrains?.constant = -(codeViewHeightConstrains?.constant)!
//scanView.layoutIfNeeded()
UIView.animateWithDuration(2.0) { () -> Void in
self.scanViewTopContrains?.constant = (self.codeViewHeightConstrains?.constant)!
UIView.setAnimationRepeatCount(MAXFLOAT)
self.scanView.layoutIfNeeded()
}
}
- [x] 就先简单说到这,还有其他有趣的用法发现了再补充~