PureLayout###
siwft
聲明打造,怎么說(shuō)也得謝謝UI試試手感,那么原先OC
布局庫(kù)的支持還有么?
細(xì)細(xì)的去github搜尋了一番,
masonary
的升級(jí)版本snapKit
和繼續(xù)支持swift
的pureLayout
什么?居然還是繼續(xù)支持swift中的自動(dòng)布局?那趕緊用起來(lái)唄(一直覺(jué)得這個(gè)庫(kù)布局更順手,使用起來(lái)也是要有全都有啊,據(jù)說(shuō)作者進(jìn)了apple?)
- [x] 首先,自然是要導(dǎo)入這個(gè)三方庫(kù)
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!
這個(gè)原因是因?yàn)?swift
中新增加了cocoaframework
這個(gè)概念,所以和原來(lái)關(guān)聯(lián)的靜態(tài)庫(kù)有所不同
- [x] 導(dǎo)入庫(kù)之后,自然就打開(kāi)
workSpace
就是了.swift
中新增了命名空間這樣的概念,所以使用cocoaPods
可以避免不少?zèng)_突(類之間的沖突)
而且swift中,一次導(dǎo)入,到處使用啊,煩人的頭文件不見(jiàn)了~
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] 布局是最常見(jiàn)的使用,通過(guò)約束來(lái)做動(dòng)畫(huà)有時(shí)也是無(wú)法避免的,我是重度純代碼使用者
// 將要改動(dòng)的約束保存起來(lái)
var codeViewHeightConstrains:NSLayoutConstraint?
var scanViewTopContrains:NSLayoutConstraint?
//設(shè)置約束
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中做動(dòng)畫(huà)去了
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] 就先簡(jiǎn)單說(shuō)到這,還有其他有趣的用法發(fā)現(xiàn)了再補(bǔ)充~