Storyboard一些特性及操作技巧整理

Xcode6之后蘋(píng)果就一直極力倡導(dǎo)使用storyboard,特別隨著Xcode6 的Size ClassIBDesignable、IBInspectable;Xcode7 的Storyboard Reference推出之后,越來(lái)越多的開(kāi)發(fā)者開(kāi)始使用Storyboard。作為一個(gè)Storyboard的重度使用者,在這里對(duì)使用Storyboard的一些特性和技巧做一些整理。

Storyboard Reference

作為xib和storyboard的重度使用者(特別是Xcode6以后),以前storyboard一直被大家吐槽,特別是多人開(kāi)發(fā)Merge的時(shí)候,一大堆warning、error撲面而來(lái),弄得頭都大。而Xcode7之后出現(xiàn)的Storyboard Reference,讓我為之一喜。

Storyboard Reference的目的是把一個(gè)個(gè)模塊分離,既避免了Main.storyboard的龐大不可把控,又模塊化UI讓分工變得簡(jiǎn)單,而且可以快速準(zhǔn)確地找到分工的各部分。

使用Storyboard Reference,最后你看到你的storyboard應(yīng)該是這樣的:

Storyboard Reference

這樣是不是簡(jiǎn)潔明了,可控性好很多,不用再為多人開(kāi)發(fā)整合而煩惱了~ ~

操作:

只需在Main.storyboard中選中要分離的UIViewController,然后點(diǎn)擊菜單:Editor->Refactor to Storyboard 創(chuàng)建一個(gè)新的分離出來(lái)的storyboard就好了。

Size Class

Xcode6之后出現(xiàn)的Size Class,下面顯示了iOS設(shè)備及其對(duì)應(yīng)的Size Class:

更直觀的iOS設(shè)備對(duì)應(yīng)的Size Class:

至于size class在各設(shè)備上的相對(duì)應(yīng)的使用就不贅述了,接下來(lái)介紹size classAttributes Inspector上的使用:

Attributes Inspector中左邊帶+的都可以使用size class.

IBDesignable&IBInspectable

IBDesignable&IBInspectable的介紹在nshipster中很早之前就有了,這里也只是我之前使用過(guò)的一些整理。

比如,在Storyboard中給按鈕加圓角、邊框什么的,User Defined Runtime Attributes可能是這個(gè)樣子的:

上圖是用戶自定義的運(yùn)行時(shí)屬性的使用,而IBInspectable屬性提供了一種新的方式。

創(chuàng)建一個(gè)自定義的UIButton:

.h文件

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface MyDrawButton : UIButton

@property (nonatomic) IBInspectable UIColor *fillColor;           //填充顏色
@property (nonatomic) IBInspectable NSInteger lineWidth;          //線的寬度
@property (nonatomic) IBInspectable NSInteger cornerRadius;       //圓角弧度
@property (nonatomic) IBInspectable NSInteger borderWidth;        //邊框?qū)挾?
@end

.m文件

#import "MyDrawButton.h"
#import <pop/POP.h>

@implementation MyDrawButton

- (void)drawRect:(CGRect)rect {
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGRect myFrame=self.bounds;
    CGContextSetLineWidth(context, _lineWidth);
    CGRectInset(myFrame, 5, 5);
    [_fillColor set];
    UIRectFrame(myFrame);
    
    self.layer.cornerRadius=_cornerRadius;
    self.layer.borderWidth=_borderWidth;
    self.layer.borderColor=_fillColor.CGColor;
    
    [self addTarget:self action:@selector(scaleToSmall)
   forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
    [self addTarget:self action:@selector(scaleAnimation)
   forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(scaleToDefault)
   forControlEvents:UIControlEventTouchDragExit];
    
}
- (void)scaleToSmall
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.95f, 0.95f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSmallAnimation"];
}
- (void)scaleAnimation
{
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(2.f, 2.f)];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.springBounciness = 25.0f;
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
    
}
- (void)scaleToDefault
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
}
@end

storyboard中的UIButton只需繼承自它,然后就可以在Attributes Inspector中看到這些屬性:

給相應(yīng)的屬性設(shè)值就有效果了。而上面給自定義按鈕添加了點(diǎn)擊動(dòng)畫(huà),只要繼承自這個(gè)自定義按鈕都會(huì)有點(diǎn)擊動(dòng)畫(huà),其效果是這樣的:

User Defined Runtime Attributes

上面圖中最后兩個(gè)是自定義關(guān)聯(lián)的運(yùn)行時(shí)屬性,
比如layer.colorFromUIColor,只需新建一個(gè)CALayerCategory

.h文件

@interface CALayer (Addition)

@property(nonatomic,strong) UIColor *colorFromUIColor;

-(void)setColorFromUIColor:(UIColor *)color;

@end

.m文件

#import "CALayer+Addition.h"
#import <objc/runtime.h>

@implementation CALayer (Addition)
@dynamic colorFromUIColor;

-(UIColor *)colorFromUIColor
{
    return objc_getAssociatedObject(self, @selector(colorFromUIColor));
}
-(void)setColorFromUIColor:(UIColor *)color
{
    objc_setAssociatedObject(self, @selector(colorFromUIColor), color, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setColorFromUI:self.colorFromUIColor];
}
-(void)setColorFromUI:(UIColor *)color
{
    self.borderColor=color.CGColor;
}
@end

這樣設(shè)置就ok了,然后在User Defined Runtime Attributes中就可以設(shè)置colorFromUIColor這個(gè)屬性了。

而自定義runtime屬性bindingAnimationType來(lái)自FastAnimationWithPOP,配置相關(guān)屬性運(yùn)行可以直接得到如下效果:

配置
導(dǎo)入pop、FastAnimationWithPOP:

pod 'pop', '~> 1.0.9'
pod 'FastAnimationWithPOP', '~> 0.0.2'

然后在Identity Inspector中配置控件的用戶自定義運(yùn)行時(shí)屬性

控制好animationTypedelay就得到了上面的效果,自己不需要寫(xiě)一句代碼!有沒(méi)有很nice! 反正我最開(kāi)始用的時(shí)候興奮極了 ~ ~

UIStackView

iOS9之后出來(lái)的UIStackView比較特別,對(duì)于自動(dòng)布局這塊非常方便,它可以快速地在垂直或水平排布多個(gè)subview,最有用的就是它會(huì)自動(dòng)為每個(gè)subview創(chuàng)建和添加Auto Layout constraints。當(dāng)然你可以控制subview的大小和位置??梢酝ㄟ^(guò)選項(xiàng)配置subview的大小、排布以及彼此間的間距。具體細(xì)節(jié)見(jiàn)iOS 9: UIStackView入門(mén),以下是我自己實(shí)戰(zhàn)操作。

UIStackView
UIStackView

至于兼容iOS9之前版本,推薦使用FDStackView,兼容UIStackView iOS9之前版本,同樣的,也可以使用storyboard,非常好!
FDStackView Part1FDStackView Part2、FDStackView Part3這三篇文章是作者分析FDStackView的設(shè)計(jì)實(shí)現(xiàn)過(guò)程,有興趣可以看一下。

提高Interface Builder高效工作的8個(gè)技巧

  • 使view的Size與view中的Content相適應(yīng)
  • 按住option鍵—觀察所選中view與另外view邊緣之間的距離
  • Editor -> Embed In View, Unembed:
  • 在不影響subview的位置時(shí)給view自由的添加padding
  • 對(duì)不在最前端的view進(jìn)行移動(dòng)
  • IBOutletCollection排序
  • 使用自定義屬性
  • MoarFonts——字體定制:所見(jiàn)即所得

具體細(xì)節(jié)查看原文:提高Interface Builder高效工作的8個(gè)技巧

總結(jié):

我現(xiàn)在一直在使用storyboard,至今感覺(jué)良好!特別有了這些技巧和特性之后,效率那是杠杠的.。這里把以前用過(guò)的寫(xiě)在這里分享?出來(lái),也方便我自己總結(jié)和回顧。

這篇文章主要內(nèi)容:

  • Storyboard Reference:Storyboard分離
  • Size Class:多設(shè)備自動(dòng)布局
  • IBDesignable&IBInspectable:配置自定義控件
  • User Defined Runtime Attributes:用戶自定義運(yùn)行時(shí)屬性
  • UIStackView:方便垂直或水平排布多個(gè)subview
  • 提高Interface Builder高效工作的8個(gè)技巧:Interface Builder的操作技巧
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,136評(píng)論 4 61
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,527評(píng)論 19 139
  • 我希望有一個(gè)如你一般的人,不是已經(jīng)屬于別人,而是屬于我或者被我無(wú)所顧及的追隨。你金燦燦的或者個(gè)人照頭像都讓我歡心,...
    tristeolive閱讀 564評(píng)論 0 0
  • 文|沒(méi)有春 我想你了 酸酸的 像剛喝了一碗醋 酸烈激靈過(guò)后 有種想哭的感覺(jué) 我想你了 暈暈的 像剛喝完一口烈酒 走...
    沒(méi)有春閱讀 571評(píng)論 22 13
  • 文/劉彩霞 第二天一早,雪在空中飄著。奇異的是太陽(yáng)出來(lái)了,只才一會(huì)兒。陽(yáng)光清麗,把雪花映襯得晶晶閃閃,雪花打著...
    彩霞漫天閱讀 387評(píng)論 0 0

友情鏈接更多精彩內(nèi)容