swift中的布局库选择:pureLayout

2016-02-23  本文已影响717人  焚琴煮鹤de我

PureLayout###

siwft声明打造,怎么说也得谢谢UI试试手感,那么原先OC布局库的支持还有么?

细细的去github搜寻了一番,masonary的升级版本snapKit和继续支持swiftpureLayout

什么?居然还是继续支持swift中的自动布局?那赶紧用起来呗(一直觉得这个库布局更顺手,使用起来也是要有全都有啊,据说作者进了apple?)

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这个概念,所以和原来关联的静态库有所不同

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()//充满
    }
}
// 将要改动的约束保存起来
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()
        }
    }
    
上一篇下一篇

猜你喜欢

热点阅读