国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

開源 Swift AutoLayout 框架 SnapKit 介紹

2019-11-09 16:21:59
字體:
來源:轉載
供稿:網友

轉自:http://www.cnblogs.com/missingcat92/p/4686248.html?utm_source=tuicool&utm_medium=referral

自從水果發(fā)布了5代,蘋果為了適配多種屏幕尺寸,搞出了一個所謂的AutoLayout來解決問題,iOS程序員們從此走上了苦逼的適配路。

“適配”本身其實并不是一個頭疼的事情,對于4-6寸的手機來說,只要制定一套規(guī)則,就不會有太大的問題。但是令我痛苦的部分在于——iOS的 Constraints 和 VFL。

痛苦的NSLayoutConstraints

這里借用iOS開源項目 Masonry 的描述,假如我們要讓一個view在superview里居中,然后每條邊留出10像素的空白,代碼是這樣寫的:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);[superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]];

如果你剛剛接觸iOS開發(fā),你可能認為這并不是什么問題。但是讓我們做個對比,在web開發(fā)中,應該怎么寫呢?

#view{margin:10px;}

一行代碼搞定問題,CSS是世界上最偉大的排版引擎,沒有之一。明明很簡單的事情,為什么放到iOS上,竟然變得這么這么這么復雜了?

仍然痛苦的VFL

然后蘋果又搞出了VFL,網上對此描述最多的是“象形文字”,雖然它真的真的改善了我們創(chuàng)建constraints的方式,讓這一切變得更快更方便,但是對于下面這段代碼,我仍然是不滿的。

constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:[trackBtn]-15-[filterBtn(40)]-15-|", options: NSLayoutFormatOptions.AlignAllRight, metrics: nil, views: views)constraints += NSLayoutConstraint.constraintsWithVisualFormat("[trackBtn]-15-|", options: NSLayoutFormatOptions.AlignmentMask, metrics: nil, views: views)constraints += NSLayoutConstraint.constraintsWithVisualFormat("|-15-[filterBtn]-15-|", options: NSLayoutFormatOptions.AlignmentMask, metrics: nil, views: views)

莫名其妙的options,metrics和views參數的組合,長成狗一樣的函數名。而且即使是有了VFL,我們還是離不開 constraints ,VFL并沒有真的滿足我們的所有需求,比如你看看 這個需求。

那么,我們是不是只能一聲嘆氣,說,iOS的世界就這樣了呢?

NO!!!我們有Masonry和SnapKit!!!

SnapKit能搞定什么

SnapKit是Masonry的Swift版,項目發(fā)布至今大約1年的時間,已經在github上有兩千多個star,當然了,這其中也少不了”他爹”Masonry給他打的那些廣告。

如果你還記得你在 StoryBoard 里面拉的那些線條,那你也一定記得其中 constriants 的表示方法:

這里寫圖片描述

SnapKit所做的就是這樣一件事——讓你這樣寫 constraints。

我們看看文章最頂部的那個需求用 SnapKit 如何實現:

view1.snp_makeConstraints { (make) -> Void in make.top.equalTo(superview).offset(10) make.left.equalTo(superview).offset(10) make.bottom.equalTo(superview).offset(-10) make.right.equalTo(superview).offset(-10)}

對!就是這么簡單!還可以更簡單!!

view1.snp_makeConstraints { (make) -> Void in make.edges.equalTo(superview).insets(UIEdgeInsetsMake(10, 10, 10, 10))}

這里寫圖片描述

(注:作者只發(fā)了一張表情

這里寫圖片描述

來,請跟我一起大叫三聲爽!

爽!

爽!

叫完之后我們繼續(xù)看還能怎么爽:

// 不只是相等哦,大于等于也是有的make.centerX.lessThanOrEqualTo(view2.snp_left)make.left.greaterThanOrEqualTo(label.snp_left)// 不止是邊緣哦,寬度高度也是有的// width >= 200 && width <= 400make.width.greaterThanOrEqualTo(200)make.width.lessThanOrEqualTo(400)// 其實可以簡單點,一行搞定一個sizemake.size.equalTo(CGSizeMake(50, 100))// 鏈式操作,優(yōu)先級想怎么搞就怎么搞make.left.greaterThanOrEqualTo(label.snp_left).PRiorityLow()// 媽媽再也不擔心我不會排版了,什么向左5像素向下10像素我一行代碼就搞定啦make.center.equalTo(superview).offset(CGPointMake(-5, 10))

做起動畫來也是一把好手!

view1.snp_makeConstraints { (make) -> Void in self.topConstraint = make.top.equalTo(superview).offset(padding.top).constraint make.left.equalTo(superview).offset(padding.left)}// then later you can callself.topConstraint.uninstall()// or if you want to update the constraintself.topConstraint.updateOffset(5)// 也可以用 snp_updateConstraints 實現上述需求

這么好的東西,我去哪里搞!

如果你用CocoaPods,那么Podfile你懂的:

pod 'SnapKit', :git => 'https://github.com/SnapKit/SnapKit.git', :branch => 'swift-2.0'

如果你用Carthege……這個我沒用過我就不瞎說了……

總之呢,不用CocoaPods的朋友可以去 這里 下載,至于怎么弄進項目我就不管了(好好滴干嘛不用CocoaPods..)

成果

我用SnapKit做了下面這個界面:

這里寫圖片描述

體驗之后發(fā)現,其實用SnapKit和原生constraints的代碼行數差不多,但是每一行從原來的極為冗長的一坨東西,變成了很容易閱讀和維護的短小精干的代碼,代碼像下面這樣優(yōu)雅,這便是SnapKit最大的作用。

mapView.snp_makeConstraints { (make) -> Void in make.edges.equalTo(view)}locateImage.snp_makeConstraints { (make) -> Void in let topHeight = navigationController!.navigationBar.frame.height + UIapplication.sharedApplication().statusBarFrame.height make.centerX.equalTo(mapView) make.bottom.equalTo(mapView.snp_centerY).offset(topHeight/2)}filterBtn.snp_makeConstraints { (make) -> Void in make.left.equalTo(mapView).offset(15) make.right.equalTo(mapView).offset(-15) make.height.equalTo(40) make.bottom.equalTo(mapView).offset(-15)}trackBtn.snp_makeConstraints { (make) -> Void in make.bottom.equalTo(filterBtn.snp_top).offset(-15) make.right.equalTo(mapView).offset(-15)}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东方市| 永嘉县| 泾源县| 新宾| 申扎县| 水富县| 宜昌市| 莱州市| 靖宇县| 万州区| 呼和浩特市| 高雄县| 霍林郭勒市| 阳城县| 读书| 龙州县| 桓台县| 大名县| 宜良县| 台前县| 沧州市| 阿坝县| 荥阳市| 邯郸市| 新安县| 渭南市| 苏州市| 石首市| 大石桥市| 渝北区| 古丈县| 元阳县| 务川| 丹寨县| 龙川县| 辽中县| 胶南市| 三原县| 沙坪坝区| 元谋县| 沙洋县|