本文主要講解三個(gè)運(yùn)算符 左移(<<)延欠、與(&)、或(|) ?在iOS代碼中如何使用仙辟。
我們經(jīng)常能看到下面這樣的代碼
UIView*view = [[UIViewalloc]init];
view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.viewaddSubview:view];
意思是撑碴,自動(dòng)調(diào)整自己的寬度,保證與superView左邊和右邊的距離不變汽馋。自動(dòng)調(diào)整自己的高度,保證與superView頂部和底部的距離不變圈盔。查看autoresizingMask的類(lèi)型豹芯,其實(shí)是NSUInteger 如下
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone ? ? ? ? ? ? ? ? =0,
UIViewAutoresizingFlexibleLeftMargin ? =1<<0,
UIViewAutoresizingFlexibleWidth ? ? ? ?=1<<1,
UIViewAutoresizingFlexibleRightMargin ?=1<<2,
UIViewAutoresizingFlexibleTopMargin ? ?=1<<3,
UIViewAutoresizingFlexibleHeight ? ? ? =1<<4,
UIViewAutoresizingFlexibleBottomMargin =1<<5
};
那么一個(gè)NSUInteger的類(lèi)型,是怎樣告訴View驱敲,既要調(diào)整自己的寬度铁蹈,又要調(diào)整自己的高度的呢?
左移運(yùn)算符 << ? ?
公式 x << 3 就是把x的各二進(jìn)位左移3位
1<<1 ?實(shí)際就是 ?0001 << 1 ?= 0010 ? 轉(zhuǎn)成十進(jìn)制后就是 ?2
1<<4 ?實(shí)際就是 ?0001 << 4 ?= 10000 ?轉(zhuǎn)成十進(jìn)制后就是 ?16
UIViewAutoresizingFlexibleWidth ? = ?00010
UIViewAutoresizingFlexibleHeight = 10000
或運(yùn)算符 |
只要對(duì)應(yīng)的二個(gè)二進(jìn)位有一個(gè)為1時(shí)众眨,結(jié)果位就為1
00010 | 10000 ?= 10010
UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight 就是
00010 | 10000 = 10010
autoresizingMask 的結(jié)果就是二進(jìn)制數(shù) 10010握牧,我們?nèi)绾沃?是哪個(gè)選項(xiàng)被選中了?
與運(yùn)算符 &
只有對(duì)應(yīng)的二個(gè)二進(jìn)位都為1時(shí)围辙,結(jié)果位才是1
10010 & 00010 = 00010
判斷選項(xiàng)是否被選中的方法如下
if(view.autoresizingMask&UIViewAutoresizingFlexibleWidth){
?NSLog(@"UIViewAutoresizingFlexibleWidth被選中了"); //code1
}
autoresizingMask = 10010
UIViewAutoresizingFlexibleWidth ?= 00010
10010 & 00010 = 00010 等價(jià)于十進(jìn)制 2我碟,為真,所以code1被執(zhí)行姚建。
if(view.autoresizingMask&UIViewAutoresizingFlexibleHeight){
NSLog(@"UIViewAutoresizingFlexibleHeight被選中了");//code2
}
autoresizingMask = 10010
UIViewAutoresizingFlexibleWidth ?= 10000
10010 & 10000 = 10000 等價(jià)于十進(jìn)制 16矫俺,為真,所以code2被執(zhí)行掸冤。
if(view.autoresizingMask&UIViewAutoresizingFlexibleLeftMargin){
NSLog(@"UIViewAutoresizingFlexibleLeftMargin");//code3
}
autoresizingMask = 10010
UIViewAutoresizingFlexibleWidth ?= 00001
10010 & 00001 = 00000 等價(jià)于十進(jìn)制 0厘托,為假,所以code3不會(huì)被執(zhí)行稿湿。
這樣我們就知道autoresizingMask 是怎么起作用的了铅匹。
實(shí)戰(zhàn)演練:
在圖文混排時(shí),我么通常需要在一段文字中饺藤,檢測(cè)是否有網(wǎng)絡(luò)地址URL, 是否有表情符號(hào)Emoji,是否有電話號(hào)碼包斑,是否有郵箱等等流礁。
typedef NS_OPTIONS(NSUInteger, FWDetectorTypes) {
FWDetectorTypesNone=0,
FWDetectorTypesURL=1<<0, ?//1
FWDetectorTypesEmoji=1<<1, ?//2
FWDetectorTypesPhoneNumber=1<<2, ?//4
FWDetectorTypesEmail=1<<3, ?//8
};
/**
需要識(shí)別的類(lèi)型:默認(rèn)識(shí)別表情
**/
@property(nonatomic,assign)FWDetectorTypes types;
如果我們只需要識(shí)別表情
types =FWDetectorTypesEmoji;
如果我們需要同時(shí)識(shí)別表情,電話罗丰,鏈接
types =FWDetectorTypesEmoji |FWDetectorTypesURL|FWDetectorTypesPhoneNumber
內(nèi)部邏輯處理時(shí):
if(types & FWDetectorTypesURL){
//識(shí)別鏈接邏輯代碼
}
if(types & FWDetectorTypesEmoji){
//識(shí)別表情邏輯代碼
}
if(types & FWDetectorTypesPhoneNumber){
//識(shí)別電話號(hào)碼邏輯代碼
}
if(types & FWDetectorTypesEmail){
//識(shí)別郵箱邏輯代碼
}
明白三個(gè)運(yùn)算符 左移(<<)神帅、與(&)、或(|) 的運(yùn)算法則萌抵,就能理解UIView的屬性autoresizingMask
一個(gè)NSUInteger類(lèi)型找御,為什么能枚舉出許多種類(lèi)型了。