麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

開源 Swift AutoLayout 框架 SnapKit 介紹

2019-11-09 16:21:59
字體:
供稿:網(wǎng)友

轉(zhuǎn)自: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ā)中,應(yīng)該怎么寫呢?

#view{margin:10px;}

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

仍然痛苦的VFL

然后蘋果又搞出了VFL,網(wǎng)上對此描述最多的是“象形文字”,雖然它真的真的改善了我們創(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參數(shù)的組合,長成狗一樣的函數(shù)名。而且即使是有了VFL,我們還是離不開 constraints ,VFL并沒有真的滿足我們的所有需求,比如你看看 這個需求。

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

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

SnapKit能搞定什么

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

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

這里寫圖片描述

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

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

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))// 鏈?zhǔn)讲僮?,?yōu)先級想怎么搞就怎么搞make.left.greaterThanOrEqualTo(label.snp_left).PRiorityLow()// 媽媽再也不擔(dān)心我不會排版了,什么向左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 實現(xiàn)上述需求

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

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

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

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

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

成果

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

這里寫圖片描述

體驗之后發(fā)現(xiàn),其實用SnapKit和原生constraints的代碼行數(shù)差不多,但是每一行從原來的極為冗長的一坨東西,變成了很容易閱讀和維護的短小精干的代碼,代碼像下面這樣優(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ā)表
主站蜘蛛池模板: 午夜色片 | 免费国产一级特黄久久 | av在线观 | 羞羞羞网站 | 欧美wwwwww| 亚洲操比视频 | xxxxhdvideosex | 国产精品视频免费网站 | 国产亚洲精久久久久久蜜臀 | 成人电影毛片 | 最新中文字幕免费视频 | 中文字幕在线看第二 | 午夜小视频免费观看 | 香蕉久久久精品 | 亚洲人成在线播放网站 | 人人玩人人爽 | 在线成人亚洲 | 九九热视频免费在线观看 | 黄片毛片一级 | 精品久久久久久久久久久久包黑料 | 国产日韩成人 | 狠狠干b| 2019天天干夜夜操 | 国产精品99久久久久久久女警 | 九九热国产视频 | 国产一国产精品一级毛片 | 婷婷亚洲一区二区三区 | 亚洲一区二区在线 | 亚洲一区二区三区91 | 91国内精品久久久久免费影院 | 国产精品亚洲精品日韩已方 | 激情大乳女做爰办公室韩国 | 久久国产成人午夜av浪潮 | 欧美视频一区二区三区在线观看 | 九一国产精品 | 国产精品久久久毛片 | 毛片在线免费观看完整版 | 永久免费不卡在线观看黄网站 | 久久草草影视免费网 | av成人在线免费观看 | 孕妇体内谢精满日本电影 |