成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

實時顯示iOS編寫UI代碼效果

移動開發
編寫iOS應用UI的方式大概有兩種,一種是Storyboard/Xib,另一種是手寫代碼。采用Storyboard/Xib方式組織UI,由于提供可視化的特性,只要從UI庫中拖動UI控件,便可以顯示結果,極大地提高開發速度。但面臨一個問題就是多人協作開發,由于所有的UI都放在同一個Storyboard文件中,使用Git/SVN合并代碼就會出現沖突。多人協作開發還不是主要問題,有人提出可以創建多個Storyboard來分開UI編寫,而Storyboard/Xib最主要問題是代碼復用性比較差。所以有些人就選擇手寫UI代碼,這樣不僅可以解決多人協作開發問題,而且通過自定義控件在多個View使

[[144355]]

編寫iOS應用UI的方式大概有兩種,一種是Storyboard/Xib,另一種是手寫代碼。采用Storyboard/Xib方式組織UI,由于提供可視化的特性,只要從UI庫中拖動UI控件,便可以顯示結果,極大地提高開發速度。但面臨一個問題就是多人協作開發,由于所有的UI都放在同一個Storyboard文件中,使用Git/SVN合并代碼就會出現沖突。多人協作開發還不是主要問題,有人提出可以創建多個Storyboard來分開UI編寫,而Storyboard/Xib最主要問題是代碼復用性比較差。所以有些人就選擇手寫UI代碼,這樣不僅可以解決多人協作開發問題,而且通過自定義控件在多個View使用。但每次手寫UI代碼后都要編譯、構建和運行,***在模擬器顯示,這樣會拖慢開發速度。如果每次修改UI控件后,保存修改便實時在模擬器顯示修改后結果,就可以極大的提高編寫UI的速度。

Live Change.gif

 

Auto Layout

Auto Layout是什么

Auto Layout是一個基于constraint(約束)的布局系統,它根據UI元素之間約束關系來調整UI元素的位置和大小。

Auto Layout解決什么問題

  • 更容易適配不同分辨率設備的屏幕(iPhone 6 Plus, iPhone 6, iPhone 5s/5, iPhone 4s/4)
  • 當設備旋轉時不需要做額外處理
  • 使用constraint來描述布局邏輯,更利于理解和清晰

如何使用Auto Layout

Auto Layout中約束的類對應是NSLayoutConstraint, 而創建NSLayoutConstraint對象主要有兩種方式,***種是

  1. + (id)constraintWithItem:(id)view1 
  2.                attribute:(NSLayoutAttribute)attribute1 
  3.                relatedBy:(NSLayoutRelation)relation 
  4.                   toItem:(id)view2 
  5.                attribute:(NSLayoutAttribute)attribute2 
  6.               multiplier:(CGFloat)multiplier 
  7.                 constant:(CGFloat)constant; 

上面方法主要意思是,某個view1的attribute1等于(小于或等于/大于或等于)某個view2的attribute2的multiplier倍加上constant。而attribute主要由表示位置(上/下/左/右)和大小(寬/高)的以下幾個值:

  1. typedef enum: NSInteger { 
  2.    NSLayoutAttributeLeft = 1
  3.    NSLayoutAttributeRight, 
  4.    NSLayoutAttributeTop, 
  5.    NSLayoutAttributeBottom, 
  6.    NSLayoutAttributeLeading, 
  7.    NSLayoutAttributeTrailing, 
  8.    NSLayoutAttributeWidth, 
  9.    NSLayoutAttributeHeight, 
  10.    NSLayoutAttributeCenterX, 
  11.    NSLayoutAttributeCenterY, 
  12.    NSLayoutAttributeBaseline, 
  13.    NSLayoutAttributeNotAnAttribute = 0 
  14. } NSLayoutAttribute; 

簡化一下,使用公式可以表達為:

  1. view1.attribute1 = view2.attribute2 * multiplier + constant 

第二種方式是:

  1. + (NSArray *)constraintsWithVisualFormat:(NSString *)format  
  2.                                  options:(NSLayoutFormatOptions)opts  
  3.                                  metrics:(NSDictionary *)metrics  
  4.                                    views:(NSDictionary *)views; 

這種方式主要是采用Visual Format Language(可視化格式語言)來描述約束布局,雖然語法比較簡潔,但是可讀性比較差和容易出錯。

Auto Layout存在問題

雖然Auto Layout在布局view方面是非常強大和靈活,但是創建constraint的語法過于繁雜,引用Masonry一個例子:

  1. UIView *superview = self; 
  2.  
  3. UIView *view1 = [[UIView alloc] init]; 
  4. view1.translatesAutoresizingMaskIntoConstraints = NO; 
  5. view1.backgroundColor = [UIColor greenColor]; 
  6. [superview addSubview:view1]; 
  7.  
  8. UIEdgeInsets padding = UIEdgeInsetsMake(10101010); 
  9.  
  10. [superview addConstraints:@[ 
  11.  
  12.     //view1 constraints 
  13.     [NSLayoutConstraint constraintWithItem:view1 
  14.                                  attribute:NSLayoutAttributeTop 
  15.                                  relatedBy:NSLayoutRelationEqual 
  16.                                     toItem:superview 
  17.                                  attribute:NSLayoutAttributeTop 
  18.                                 multiplier:1.0 
  19.                                   constant:padding.top], 
  20.  
  21.     [NSLayoutConstraint constraintWithItem:view1 
  22.                                  attribute:NSLayoutAttributeLeft 
  23.                                  relatedBy:NSLayoutRelationEqual 
  24.                                     toItem:superview 
  25.                                  attribute:NSLayoutAttributeLeft 
  26.                                 multiplier:1.0 
  27.                                   constant:padding.left], 
  28.  
  29.     [NSLayoutConstraint constraintWithItem:view1 
  30.                                  attribute:NSLayoutAttributeBottom 
  31.                                  relatedBy:NSLayoutRelationEqual 
  32.                                     toItem:superview 
  33.                                  attribute:NSLayoutAttributeBottom 
  34.                                 multiplier:1.0 
  35.                                   constant:-padding.bottom], 
  36.  
  37.     [NSLayoutConstraint constraintWithItem:view1 
  38.                                  attribute:NSLayoutAttributeRight 
  39.                                  relatedBy:NSLayoutRelationEqual 
  40.                                     toItem:superview 
  41.                                  attribute:NSLayoutAttributeRight 
  42.                                 multiplier:1 
  43.                                   constant:-padding.right], 
  44.  
  45.  ]]; 

如此簡單的一個例子都要編寫這么多行代碼,想象一下如果創建多個view的constraint時會多么痛苦啊。另一個方式是采用Visual Format Language (VFL),雖然語法比較簡潔,但是可讀性比較差和容易出錯。

Masonry

為什么使用Masonry

Masonry是采用鏈式DSL(Domain-specific language)來封裝NSLayoutConstraint,通過這種方式編寫Auto Layout布局代碼更加易讀和簡潔。
使用Masonry的MASConstraintMaker來表達相同constraint

  1. UIEdgeInsets padding = UIEdgeInsetsMake(10101010); 
  2.  
  3. [view1 mas_makeConstraints:^(MASConstraintMaker *make) { 
  4.     make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler 
  5.     make.left.equalTo(superview.mas_left).with.offset(padding.left); 
  6.     make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom); 
  7.     make.right.equalTo(superview.mas_right).with.offset(-padding.right); 
  8. }]; 

甚至可以更短

  1. [view1 mas_makeConstraints:^(MASConstraintMaker *make) { 
  2.     make.edges.equalTo(superview).with.insets(padding); 
  3. }]; 

#p#

如何使用

使用Masonry創建constraint來定義布局的方式有三種:mas_makeConstraints,mas_updateConstraints,mas_remakeConstraints。

1. mas_makeConstraints

使用mas_makeConstraints創建constraint后,你可以使用局部變量或屬性來保存以便下次引用它;如果創建多個constraints,你可以采用數組來保存它們。

  1. // in public/private interface 
  2. @property (nonatomic, strong) MASConstraint *topConstraint; 
  3.  
  4. ... 
  5.  
  6. // when making constraints 
  7. [view1 mas_makeConstraints:^(MASConstraintMaker *make) { 
  8.     self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top); 
  9.     make.left.equalTo(superview.mas_left).with.offset(padding.left); 
  10. }]; 
  11.  
  12. ... 
  13. // then later you can call 
  14. [self.topConstraint uninstall]; 

2. mas_updateConstraints

有時你需要更新constraint(例如,動畫和調試)而不是創建固定constraint,可以使用mas_updateConstraints方法

  1. // this is Apple's recommended place for adding/updating constraints 
  2. // this method can get called multiple times in response to setNeedsUpdateConstraints 
  3. // which can be called by UIKit internally or in your code if you need to trigger an update to your constraints 
  4. - (void)updateConstraints { 
  5.     [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) { 
  6.         make.center.equalTo(self); 
  7.         make.width.equalTo(@(self.buttonSize.width)).priorityLow(); 
  8.         make.height.equalTo(@(self.buttonSize.height)).priorityLow(); 
  9.         make.width.lessThanOrEqualTo(self); 
  10.         make.height.lessThanOrEqualTo(self); 
  11.     }]; 
  12.  
  13.     //according to apple super should be called at end of method 
  14.     [super updateConstraints]; 

3. mas_remakeConstraints

mas_remakeConstraints與mas_updateConstraints比較相似,都是更新constraint。不過,mas_remakeConstraints是刪除之前constraint,然后再添加新的constraint(適用于移動動畫);而mas_updateConstraints只是更新constraint的值。

  1. - (void)changeButtonPosition { 
  2.     [self.button mas_remakeConstraints:^(MASConstraintMaker *make) { 
  3.         make.size.equalTo(self.buttonSize); 
  4.  
  5.         if (topLeft) { 
  6.             make.top.and.left.offset(10); 
  7.         } else { 
  8.             make.bottom.and.right.offset(-10); 
  9.         } 
  10.     }]; 

想了解以上三個代碼片段的更多細節,可以下載Masonry iOS Examples工程查閱。

Classy

Classy簡介和特性

Classy是一個能與UIKit無縫結合stylesheet(樣式)系統。它借鑒CSS的思想,但引入新的語法和命名規則。

靈活內嵌的語法

{ } : ; 這些語法符號是可選的,你可以選擇適合自己的風格來表達stylesheet。

你可以使用{ } : ; 來限定stylesheet

  1. $main-color = #e1e1e1; 
  2.  
  3. MYCustomView { 
  4.   background-color: $main-color; 
  5.   title-insets: 510510
  6.   > UIProgressView.tinted { 
  7.     progress-tint-color: black; 
  8.     track-tint-color: yellow; 
  9.   } 
  10.  
  11. ^UIButton.warning, UIView.warning ^UIButton { 
  12.   title-color[state:highlighted]: #e3e3e3; 

或者你使用空格來限定stylesheet

  1. $main-color = #e1e1e1 
  2.  
  3. MYCustomView  
  4.   background-color $main-color 
  5.   title-insets 510510 
  6.   > UIProgressView.tinted  
  7.     progress-tint-color black 
  8.     track-tint-color yellow 
  9.  
  10. ^UIButton.warning, UIView.warning ^UIButton  
  11.   title-color[state:highlighted] #e3e3e3 

默認樣式

Classy在應用程序Bundle默認查找文件名為stylesheet.cas的樣式文件。如果你采用這個文件名,你可以不用做任何東西就能加載樣式文件。
但如果你想指定其他file path(樣式文件名),你可以創建[CASStyler defaultStyler]

  1. [CASStyler defaultStyler].filePath = [[NSBundle mainBundle] pathForResource:@"myStyles.cas" ofType:nil]; 

如果你還想當發生錯誤時,獲取錯誤信息以便于調試,可以使用-(void)setFilePath:error:

  1. NSError *error = nil; 
  2. NSString filePath = [[NSBundle mainBundle] pathForResource:@"myStyles.cas" ofType:nil]; 
  3. [[CASStyler defaultStyler] setFilePath:filePath error:&error]; 

如果你是使用Storyboard/Xib組織UI界面,那就需要在main.m的int main(int argc, char * argv[])方法設置 filePath,這樣可以確保在創建UIWindow之前加載stylesheet。否則(采用手寫UI代碼),你在 AppDelegate.m的- (BOOL)application:didFinishLaunchingWithOptions:方法設置filePath

Live Reload

Live Reload是實時顯示編寫UI代碼效果的關鍵特性,它能夠實時檢查stylesheet文件變化,無需重新編譯、構建和運行模擬器,從而極大提高開發速度。
為了啟用Live Reload,你需要指定stylesheet路徑,并且只運行在模擬器上。

  1. #if TARGET_IPHONE_SIMULATOR 
  2.     NSString *absoluteFilePath = CASAbsoluteFilePath(@"../Styles/stylesheet.cas"); 
  3.     [CASStyler defaultStyler].watchFilePath = absoluteFilePath; 
  4. #endif 

Selectors

Style Selectors是指定哪個view使用哪種樣式的方式。主要有三種方法來指定目標view:

  1. Object Class
  2. View Hierarchy
  3. Style Class

你可以混合使用三種方法,例子如下:

  1. /* match views 
  2.  * where class is UIButton or UIButton subclass 
  3.  * and styleClass is "large" 
  4.  * and superview class is UITabBar 
  5.  */ 
  6.  
  7. UITabBar > ^UIButton.large { } 

想了解具體如何使用,請查閱官網Selectors章節

為了避免與Objective-C的message selectors混淆,術語style selectors表示Classy stylesheets的selectors

Properties

Classy支持所有UIAppearance的屬性和方法,也支持與UIAppearance無關的很多屬性。Classy使用與UIKit相同屬性命名,所以你不必考慮如何將style property映射到Objective-C的property。
UIPageControl類的屬性如下:

  1. @property (nonatomic,retain) UIColor *pageIndicatorTintColor; 
  2. @property (nonatomic,retain) UIColor *currentPageIndicatorTintColor; 

style property的名字采用與objective-c一樣的名字

  1. UIPageControl { 
  2.   pageIndicatorTintColor black 
  3.   currentPageIndicatorTintColor purple 

style property的命名規則采用kebab case

  1. UIPageControl { 
  2.   page-indicator-tint-color black 
  3.   current-page-indicator-tint-color purple 

想了解具體如何使用,請查閱官網Properties章節

Keep it DRY(Don't Repeat Yourself)

在編程中一個很重要的原則就是避免重復,這不僅可以大量減少重復代碼,并且使得代碼更加容易復用和維護。Classy提供三種方式避免代碼重復:grouping,nesting,variables

Grouping

如果有兩個以上的style selectors共用相同的屬性時

  1. UISlider.info { 
  2.   minimum-track-tint-color black 
  3.   maximum-track-tint-color purple 
  4.  
  5. UISlider.error { 
  6.   minimum-track-tint-color black 
  7.   maximum-track-tint-color purple 
  8.   thumb-tint-color red 

我們可以提取相同的屬性到分組style selector中

  1. UISlider.info, UISlider.error { 
  2.   minimum-track-tint-color black 
  3.   maximum-track-tint-color purple 
  4.  
  5. UISlider.error { 
  6.   thumb-tint-color red 

#p#

Nesting

如果兩個以上style selectors共用相同的view hierarchy時

  1. UICollectionView { 
  2.   background-color #a2a2a2 
  3.  
  4. UICollectionView > UICollectionViewCell { 
  5.   clips-to-bounds NO 
  6.  
  7. UICollectionView > UICollectionViewCell UILabel { 
  8.   text-color purple 
  9.  
  10. UICollectionView > UICollectionViewCell UILabel.title { 
  11.   font 20 

我們通過nesting方式將view hierarchies表達成這樣方式

  1. UICollectionView { 
  2.   background-color #a2a2a2 
  3.  
  4.   > UICollectionViewCell { 
  5.     clips-to-bounds NO 
  6.  
  7.     UILabel { 
  8.       text-color purple 
  9.  
  10.       &.title { 
  11.         font 20 
  12.       } 
  13.     } 
  14.   } 

Variables

Classy讓你通過定義variables來將多個相同的style property值存儲以便共享。Variable命名規則如下:

  • 必須以大小寫字母或$符號開頭
  • 可以包含_,-或任何字母數字

    1. // prefix with ' $ ' to help distinguish variables 
    2. $brand-color = #e1e1e1 
    3.  
    4. // OR not 
    5. insets = 510510 
    6.  
    7. UIButton { 
    8.   background-color $brand-color 
    9.   contentEdgeInsets insets 
    10.   background-image[state:selected] bg_button insets 

    ***官方還提供一個實例來解釋具體如何使用:Custom Views Example

ClassyLiveLayout

ClassyLiveLayout通過結合Classy stylesheets與Masonry一起使用,能夠在運行的模擬器中微調Auto Layout約束實時顯示效果的工具。

ClassyLiveLayout一個核心category:UIView+ClassyLayoutProperties,在UIView定義以下屬性:

  1. @property(nonatomic, assign) UIEdgeInsets cas_margin; 
  2. @property(nonatomic, assign) CGSize cas_size; 
  3.  
  4. // shorthand properties for setting only a single constant value 
  5. @property(nonatomic, assign) CGFloat cas_sizeWidth; 
  6. @property(nonatomic, assign) CGFloat cas_sizeHeight; 
  7.  
  8. @property(nonatomic, assign) CGFloat cas_marginTop; 
  9. @property(nonatomic, assign) CGFloat cas_marginLeft; 
  10. @property(nonatomic, assign) CGFloat cas_marginBottom; 
  11. @property(nonatomic, assign) CGFloat cas_marginRight; 

cas_margin和cas_size分別表示UI元素的位置和大小,而其余的屬性都是對兩個屬性進一步細分。我們可以從stylesheets中訪問style properties來定義constraints布局,做到將數據與代碼分離,有利于修改和復用代碼。

  1. UIView.blue-box { 
  2.     cas_size: 80 100 
  3.     cas_margin-top: 60 
  4.     cas_margin-left: 50 
  5.  
  6. UIView.red-box { 
  7.     cas_size-width: 120 
  8.     cas_margin-left: 20 

我們可以在updateConstraints或updateViewConstrains定義布局時引用style properties

  1. - (void)updateViewConstraints { 
  2.   [super updateViewConstraints]; 
  3.  
  4.   [_blueBoxView mas_updateConstraints:^(MASConstraintMaker *make) { 
  5.       make.width.equalTo(@(_blueBoxView.cas_size.width)); 
  6.       make.height.equalTo(@(_blueBoxView.cas_size.height)); 
  7.       make.top.equalTo(@(_blueBoxView.cas_margin.top)); 
  8.       make.left.equalTo(@(_blueBoxView.cas_margin.left)); 
  9.   }]; 
  10.  
  11.   [_redBoxView mas_updateConstraints:^(MASConstraintMaker *make) { 
  12.       make.width.equalTo(@(_redBoxView.cas_size.width)); 
  13.       make.height.equalTo(_blueBoxView); 
  14.       make.top.equalTo(_blueBoxView); 
  15.       make.left.equalTo(_blueBoxView.mas_right).with.offset(_redBoxView.cas_margin.left); 
  16.   }]; 

當定義view layouts時,將Auto Layout的constraints都放在stylesheets中實時加載(Live reload)。如果你修改constraints,無需重新編譯、構建和運行模擬器便能實時看到修改后的效果。

示例工程

配置工程

由于需要引用Masonry,Classy和ClassyLiveLayout,Podfile配置如下:

  1. pod 'Masonry''~> 0.6.1' 
  2. pod 'Classy''~> 0.2.4' 
  3. pod 'ClassyLiveLayout''~> 0.6.0' 

編寫代碼

1. 添加stylesheet.cas文件到工程

當安裝好Masonry,Classy和ClassyLiveLayout后,***次運行項目會出現沒有stylesheet.cas文件錯誤:

No stylesheet.cas file error.png

No stylesheet.cas file error.png

 

只要向工程添加空的stylesheet.cas文件即可。

Create stylesheet.cas file.png

Create stylesheet.cas file.png

 

2. 創建LiveView類,該類繼承SHPAbstractView。

Create LiveView inherit SHPAbstractView.png

Create LiveView inherit SHPAbstractView.png

 

在ViewController創建LiveView對象,然后被self.view引用。

Setup root view in ViewController.png

Setup root view in ViewController.png

 

當編譯運行時,在SHPAbstractView.h由于找不到UIView出現編譯錯誤。

SHPAbstractView Compile error.png

SHPAbstractView Compile error.png

 

#p#

只需引入UIKit便可以解決,但運行一下應用程序,出現一下錯誤:

Must override methods.png

Must override methods.png

 

主要原因是任何自定義UIView繼承SHPAbstractView都需要override兩個方法:- (void)addSubviews和- (void)defineLayout,我們可以查看SHPAbstractView的源碼可知:

SHPAbstractView Source Code .png

SHPAbstractView Source Code .png

 

所以只要在LiveView.m文件覆蓋兩個方法即可

  1. #pragma mark - Add subviews and define layout 
  2. - (void)addSubviews 
  3.  
  4. - (void)defineLayout 

3. LiveView類設計

LiveView主要由包含redBoxView和blueBoxView兩個屬性,redBoxView表示紅色方塊,blueBoxView表示藍色方塊。

  1. #import "SHPAbstractView.h" 
  2.  
  3. @interface LiveView : SHPAbstractView 
  4.  
  5. @property (strong, nonatomic) UIView *redBoxView; 
  6. @property (strong, nonatomic) UIView *blueBoxView; 
  7.  
  8. @end 

4. LiveView類實現

由于SHPAbstractView類如何初始化View已經做了處理,暴露兩個接口- (void)addSubviews和-(void)defineLayout分別處理構建view hierarchy和定義布局,子類只要覆蓋SHPAbstractView這兩個方法就可以創建LiveView了。
但是我們將Auto Layout的constraints都放在stylesheets中實時加載(Live reload),即放在本工程的stylesheet.cas文件,將布局數據和布局代碼分離。

  1. UIView.redBox { 
  2.     cas_marginTop 50 
  3.     cas_marginLeft 20 
  4.  
  5.     cas_size 100 100 
  6.  
  7. UIView.blueBox { 
  8.     cas_marginTop 50 
  9.     cas_marginRight -20 
  10.  
  11.     cas_size 100 100 

有了constraints數據后,便可以在代碼布局:

  1. @implementation LiveView 
  2.  
  3. #pragma mark - Add subviews and define layout 
  4. - (void)addSubviews 
  5.     self.backgroundColor = [UIColor whiteColor]; 
  6.     [self addSubview:self.redBoxView]; 
  7.     [self addSubview:self.blueBoxView]; 
  8.  
  9. - (void)defineLayout 
  10.     [self.redBoxView mas_updateConstraints:^(MASConstraintMaker* make){ 
  11.         make.top.equalTo(@(self.redBoxView.cas_marginTop)); 
  12.         make.left.equalTo(@(self.redBoxView.cas_marginLeft)); 
  13.         make.width.equalTo(@(self.redBoxView.cas_sizeWidth)); 
  14.         make.height.equalTo(@(self.redBoxView.cas_sizeHeight)); 
  15.     }]; 
  16.  
  17.     [self.blueBoxView mas_updateConstraints:^(MASConstraintMaker *make){ 
  18.         make.top.equalTo(@(self.blueBoxView.cas_marginTop)); 
  19.         make.right.equalTo(@(self.blueBoxView.cas_marginRight)); 
  20.         make.width.equalTo(@(self.blueBoxView.cas_sizeWidth)); 
  21.         make.height.equalTo(@(self.blueBoxView.cas_sizeHeight)); 
  22.     }]; 
  23.  
  24. #pragma mark - Lazy initialization 
  25. - (UIView*)redBoxView 
  26.     if (!_redBoxView) { 
  27.         _redBoxView = [UIView new]; 
  28.         _redBoxView.cas_styleClass = @"redBox"
  29.         _redBoxView.backgroundColor = [UIColor redColor]; 
  30.     } 
  31.  
  32.     return _redBoxView; 
  33.  
  34. - (UIView*)blueBoxView 
  35.     if (!_blueBoxView) { 
  36.         _blueBoxView = [UIView new]; 
  37.         _blueBoxView.cas_styleClass = @"blueBox"
  38.         _blueBoxView.backgroundColor = [UIColor blueColor]; 
  39.     } 
  40.  
  41.     return _blueBoxView; 

5. 模擬器支持Live Reload

為了啟用Live Reload,你需要指定stylesheet路徑,并且只運行在模擬器上。

Support Live Reload.png

Support Live Reload.png

 

此時效果:

Live Change.gif

 

#p#

6. 分離樣式文件

由于有網友提出這樣一個問題:如果所有view的樣式都放在同一個stylesheet.cas文件,會讓stylesheet.cas文件繁雜,并且當多個人協同開發時,不易于合并代碼,所以有必要將樣式文件分離到多個文件中。

  1. 創建variable.cas文件,并將redBox對應UIView的樣式放在variable.cas文件中。

    variable.cas file.png

    variable.cas file.png

     

  2. 在stylesheet.cas樣式文件使用@import指令引用variable.cas文件

stylesheet.cas file.png

stylesheet.cas file.png

 

***效果

Live Change 1.gif

Live Change 2.gif

 

示例代碼存放地址:LiveAutoLayout

總結

之前手寫UI代碼每次更改一般都要重新編譯、構建和運行模擬器才能看到效果,但結合使用Masonry,Classy和ClassLiveLayout之后,告別這個費時過程,極大地提高開發速度;不僅如此,我們將Auto Layout的constraints都放在stylesheets中實時加載(Live reload),將布局數據和布局代碼分離,使得代碼更加復用和維護。Classy還提供三種避免重復方法:Grouping, Nestting和Variable,盡可能復用樣式數據。
這是本人***次編寫技術博客,可能有很多錯誤和漏洞,希望大家多多指點,也希望這篇文章能夠幫助到大家。

責任編輯:倪明 來源: 簡書
相關推薦

2024-03-15 12:48:50

攜程AI

2012-04-24 23:31:41

iOS

2011-08-22 16:45:58

Windows PhoiOS

2021-01-19 12:16:10

CSS前端UI

2022-09-27 14:32:12

iOSiOS 16蘋果

2010-09-09 11:16:06

CSS交互

2020-03-17 14:26:18

iOS 14iPhone 9 Pl蘋果

2009-06-25 14:53:35

自定義UI組件JSF框架

2021-06-03 07:45:25

Rust Git 終端 UI

2011-06-28 17:21:50

QT UI designer

2010-04-29 17:22:42

UNIX系統

2009-07-10 13:20:37

Swing容器組件

2012-09-06 11:18:17

IBMdw

2012-09-10 10:31:31

IBMdw

2015-01-28 14:30:31

android代碼

2010-08-31 13:32:12

CSS

2022-06-27 06:23:23

代碼編程

2021-06-08 09:35:11

Cleaner ReaReact開發React代碼

2022-12-15 10:52:26

代碼開發

2023-06-19 14:14:24

Rust程序Web
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99国产精品视频免费观看一公开 | 日韩三级一区 | 中文字幕 在线观看 | 久久鲁视频| 91九色婷婷 | 国产免费观看视频 | 一区二区三区四区在线视频 | 欧美激情在线一区二区三区 | 日韩电影免费在线观看中文字幕 | 亚洲国产精品久久久久婷婷老年 | 天堂在线一区 | 久久99深爱久久99精品 | 久久亚洲91| 亚洲精品av在线 | 剑来高清在线观看 | 一区二区国产精品 | 国产精品视频网站 | 在线观看免费av网 | 国产精品日日做人人爱 | 久久亚洲91 | 久热电影 | 91.色 | 国产一区二区三区在线 | 一区二区免费视频 | 精品一区二区三区四区在线 | 999久久久 | 精品一区国产 | 成人精品在线观看 | 91精品久久久久久久久久入口 | 亚洲一区国产精品 | 久久午夜剧场 | 久久机热| 91精品国产日韩91久久久久久 | 国产精品乱码一区二三区小蝌蚪 | 999热视频| 欧美中文在线 | 国产精品久久久久久久午夜片 | 伊人久久免费 | yeyeav | 亚洲性在线| 99精品国产一区二区三区 |