ios筆記4

延後執行:performSelector withObject afterDelay

imageNamed內存不釋放(賦值時自動retain)

imageWithContentOfFile置爲nil時釋放內存(賦值時不retain)

self.imageView.animationImages=nil引用數清零後也能清理內存

毛玻璃效果:imageView加載圖片上addSubview,subview爲toolbar,toolbar設置透明度

顏色處理(生成UIColor對象):[UIColor colorWithRed:(243/255.0) Green:(243/255.0) Blue:(243/255.0)]

NSString *path=【NSBundle mainBundle】pathForResource:@“mysong1.mp3” ofType: nil]

NSUrl *url=[NSUrl fileWithPath:path] weak引用計數不加一(快捷方式)

strong引用計數加一(硬鏈接)

ios爲ARC架構

button的文字設置需要用setTitle forState,不能直接修改titleLabel屬性

取最後一個子組件[.subViews lastObject]

初始化數組使用@:

NSArray<NSDictionay *> *dataArr =@[

@{@"name":@"aaaa1",@"icon":@"aaaaa1.png"},

@{@"name":@"aaaa2",@"icon":@"aaaaa2.png"},

@{@"name":@"aaaa3",@"height":@3.0},

@{@"name":@"aaaa4",@"icon":@"aaaaa4.png"},

@{@"name":@"aaaa5",@"icon":@"aaaaa5.png"}

];

 

調試:

po NSHomeDirectory()

自定義控件:

1、繼承UIView

2、實現init方法(不要傳寬高)

3、實現layoutSubviews方法

在layoutSubviews方法裏一定要調用一次[super layoutSubviews]

在layoutSubviews方法裏通過

self.frame.size.with,self.frame.size.height獲得整體寬度和高度

在layoutSubviews方法中設置子組件的寬度和高度

 

新建button 必須使用[UIButton buttonWithType:]方法新建

 

1、在initWithFrame方法中添加子控件,提供便利構造方法

2、在layoutSubviews方法中設置子控件的frame(一定要調用[super layoutSubviews])

3、增加模型屬性

 

Xib的加載:

UIView *carView =【[[NSBundle mainBundle] loadNibNamed:@"xib文件名" owner:nil options:nil】firstObject

 

方式二

UINib *nib =[UINib nibWithNibName:@"" bundle:nil];

UIView *carview =[[nib instantiateWithOwner:nil options:nil]firstObject]

xib可以指定對應的View類

xib不可以通過alloc init方式創建對象,只能通過以上兩種方式創建

重寫initWithCoder方法實現初始化

 

受保護拉伸圖片:

image stretchableImageWithLeftCapWidth:imageWidth*0.5 topCapHeight: imageHeight*0.5

右側自動計算:width - leftCapWidth - 1

底部自動計算:height - topCapHeight - 1

scrollView.pagingEnable=YES開啓自動分頁功能

 

[pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"]

[pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"]

 

KVO:KV Observer 關鍵addObserver

KVC:KV Coding

 

作用:修改timer在runloop中的模式爲NSRunLoopCommonModes

目的:不管主線程在做什麼操作,都會分配一定的時間處理定時器

[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

 

Xib啓動方法:awakeFromNib

 

[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]

刪除scrollView中的所有子組件

 

autoresizing:

view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoResizingFlexibleTopMargin;

 

awakeFromNib中設置自動伸縮

 

代碼的方式滾動,可以滾動到一個不合理的位置

autolayout功能更強

使用autolayout需要禁止autoresizing

view.translateAutoresizingMaskIntoConstraints = NO;

 

VFL:Visual Format Language

NSString * hvfl =@"H:|-20-[redView]-20-|";

NSArray *hlcs = [NSLayoutConstraint constraitsWithVisualFormat:hlcs options:kNilOptions metrics:nil views:nil];

[self.view addConstraints:hlcs]

自動佈局的核心計算公式:obj1.property1 =(obj2.property2 * multiplier)*constant value

 

需要通過修改約束的方式修改位置和尺寸

約束的本質:自動轉化爲frame,當做動畫時,需要先修改約束,然後在動畫代碼中強制刷新:

self.redViewW.constraint = 50;

[UIView animateWithDuration:2.0 animations:^{

[self.view layoutIfNeeded];//強制刷新

}];

 

ios的websocket:facebook開源的 SocketRocket 框架

 

 

masonry框架用法

left對left,right對right,top對top,height對height,width對width,bottom對bottom

#import "Masonry.h"

 

[redView mas_makeConstraints:^(MASConstraintMaker *make){

make.top.equalTo(self.view.mas_top).offset(20);

make.left.equalTo(self.view.mas_left).multipliedBy(1.0).offset(20);

}];

 

make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20,20,20,20));

 

make.width.mas_equalTo(100);

make.height.mas_equalTo(100);

make.centerX.mas_equalTo(self.view);

make.centerY.mas_equalTo(self.view);

 

 

省略mas_前綴

#define MAS_SHORTHAND

#define MAS_SHORTHAND_GLOBALS

 

更新約束:

updateConstraints:

 

tableCell自動行高(ios8以後有效):

viewDidLoad{

【super viewDidLoad】;

self.tableView.rowHeight = UITableViewAutomaticDimension;

self.tableView.estimateRowHeight =44;//估算高度

}

Label:lines設置爲0自動換行

 

拉約束進入代碼變量

設置constraint=100或者constrait=0

 

注意Xcode中很多地方用到按住Control拖拉

 

GCD用來替代NSThread,充分使用多核,C語言寫的(經常使用)

 

NSOperation是oc對GCD的包裝(經常使用)

 

pthread版:pthread_create啓動線程

NSThread start啓動線程

detachNewThreadSelector

self performSelectorInBackground 啓動後臺線程

@synchronized加鎖同步操作

atomic會對set方法加鎖

nonatomic不會對set方法加鎖

線程間通信常用方法:

1、performSelectorOnMainThread

2、performSelector onThread

 

下載圖片操作:

[NSURL URLWithString]

NSData *imageData=[NSData dataWithContentsOfURL: url];

[UIImage imageWithData:imageData]

 

時間操作

NSDate

CFTimeInterval start=CFAbsoluteTimeGetCurrent();//函數調用

 

[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject: image waitUntilDone:YES];

 

GDC異步函數併發隊列

dispath_queue_t queue = dispath_queue_create("com.520it.download",DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue,^{

NSLog(@"download1----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"download2----%@",[NSThread currentThread]);

});

GDC異步函數串行隊列

dispath_queue_t queue = dispath_queue_create("com.520it.download",DISPATCH_QUEUE_SERIAL);

dispatch_async(queue,^{

NSLog(@"download1----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"download2----%@",[NSThread currentThread]);

});

 

同步函數:

dispatch_sync,不會開線程

異步函數

dispatch_async,會開線程:併發隊列,開多個線程;串行隊列開一個線程

dispatch_async 函數會將傳入的block塊放入指定的queue裏運行。這個函數是異步的,這就意味着它會立即返回而不管block是否運行結束。因此,我們可以在block裏運行各種耗時的操作(如網絡請求) 而同時不會阻塞UI線程。 

dispatch_get_global_queue 會獲取一個全局隊列,我們姑且理解爲系統爲我們開啓的一些全局線程。我們用priority指定隊列的優先級,而flag作爲保留字段備用(一般爲0)。 

dispatch_get_main_queue 會返回主隊列,也就是UI隊列。它一般用於在其它隊列中異步完成了一些工作後,需要在UI隊列中更新界面(比如上面代碼中的[self updateUIWithResult:result])的情況。

dispath_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

獲得主隊列(UI隊列)

dispath_queue_t queue =dispatch_get_main_queue();

注意:主隊列不能使用同步函數

 

其他函數:

dispatch_after

performSelector

 

dispatch_once實現單例(一次性代碼,整個程序運行過程中只會運行一次)

單例類需要重寫alloc方法和init方法

最好重寫:allocWithZone方法

@synchronized(self){

}

static dispatch_once_t onceToken;

dispatch_once(&onceToken,^{

_instance = [super allocWithZone:zone];

})

dispatch_once專爲單例設計

單例命名規則:share或default開頭

提供類方法

+(instancetype)shareTool{

return [[self alloc]init];

}

重寫copyWithZone方法

-(id)copyWithZone:(NSZone*) zone{

return _instance;

}

-(id)mutableCopyWithZone:(NSZone*) zone{

return _instance;

}

MRC下的單例

需要重寫release、retain和retainCount方法

其中,retainCount方法爲:

-(NSUInteger)retainCount{

return MAXFLOAT;

}

 

柵欄函數

dispatch_barrier_async(queue,^{

});

只有柵欄函數執行完成後纔會執行後面的隊列

 

快速迭代(迭代10次)

dispatch_apply(10,dispatch_get_global_queue(0,0),^(size_t index){

});

 

隊列組

dispatch_queue_t queue = dispatch_get_global_queue();

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group,queue,^{

});

dispatch_group_notify(group,queue,^{

});//當隊列組中所有線程執行完成後會調用此代碼塊

 

開始結束添加隊列組函數

dispatch_group_enter(group);

dispatch_async();

dispatch_group_leave(group);

 

函數方式調用:

dispatch_async_f();

 

判斷arc,mrc的宏

#if __has_feature(obj_arc)

 

NSInvocationOperation alloc initWithTarget

NSBlockOperation *oper = [NSBlockOperation blockeOperationWithBlock:^{

}];

[oper start]

兩種方式都需要調用start方法

NSBlockOperation如果block數量大於1,系統會開多個線程,不一定爲子線程,可能爲主線程

[NSOperationQueue mainQueue]主隊列和GCD中的主隊列一樣,串行隊列

[[NSOperationQueue alloc]init]非主隊列默認情況下是併發隊列

 

[queue addOperation:oper];

[queue addOperationWithBlock:^{

}];

自定義NSOperation需要實現main方法

queue不需要start

[op1 addDependency:op4];//操作依賴,不能循環依賴

op3.completionBlock=^{

};//操作監聽

 

畫圖:

UIGraphicsBeginImageContext(CGSizeMake(200,200));

[image1 drawInRect:CGRectMake(0,0,100,200)];

[image2 drawInRect:CGRectMake(100,0,100,200)];

UIImage *image=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

到主線程更新UI

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

}];

NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSCacheDirectory,NSUserDomainMask,YES) lastObject]

);打印應用運行目錄沙盒緩存路徑

 

NSData *imageData = [NSData dataWithContentsOfFile:fullpath];

if(imageData){

UIImage * image = [UIImage imageWithData:imageData];

}

 

內存告警

重寫方法:-(void)didReceiveMemoryWarning{}

-(void)applicationDidReceiveMemoryWarning{}

SDWebImage圖片加載框架

 

NSDictionary是對象的拷貝

NSCache是對象的strong引用

 

枚舉

typedef enum{

ActionToTop,

ActionToBottom,

}ActionType;

 

typedef NS_ENUM(NSInteger,ActionType){

ActionToTop,

ActionToBottom,

};

 

typedef NS_OPTIONS(NSInteger, ActionType){

ActionToTop = 1<<0,

ActionToBottom =1<<1,

}

位移枚舉:一個參數可以傳多個值

取值時按位與操作判斷:

if(aaa&ActionToTop){

}

 

定時器需要放在RunLoop裏面

【【NSRunLoop currentRunLoop 】addTimer forMode:NSLoopCommonMode】

或者

currentLoop=【NSRunLoop currentLoop】;

【NSTimer】

【currentLoop run】

 

KVC框架

[video setValuesForKeysWithDictionay: dict]

JSON序列化:

NSJSONSerialization

MJExtension

XML序列化:

NSXMLParsor SAX方式,使用簡單,佔用內存小

GDataXML DOM方式,基於libxml2

 

Build Phase:

Compile

-fno-objc-arc

 

AFNetworking

 

Base64操作在NSData類裏

 

注意:在xib中自定義TableViewCell不能勾選Custom確定行高

 

sizeToFit自動調整寬高(不會向子組件傳遞此方法)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章