注:再本文中您將可能看到排版混亂,中英文混用,表達不清等問題,敬請見諒。中英文混用是因為我想保留英文的意思,避免錯翻譯(其實是我英文水平不夠根本就不知道什么意思)。
------------------------------------------------------------- 碎 片 知 識 ----------------------------------------------------------------------------
1.data source也是一種特殊的delegate,data source負責傳輸數據給View例如data、at、count等,而delegate負責處理View的did、should、will等問題,View把需要的數據信息或者事件信息傳輸給dalegate,dalegate則負責回復這些信息。
--------------------------------------------------------------- Block -------------------------------------------------------------------------------
1.Block當中不能修改局部變量的值,在block中,局部變量都是只讀的,比如在block外部有一個BOOL value = YES;,如果在block內部修改value的值,這就是非法的,編譯器會報錯。當然,有一種辦法可以進行讀寫,就是在變量前面加上兩個下劃線和block,如:__block BOOL value = YES;,這樣就可以在block當中進行該變量的讀寫了。
int anInteger = 42;void (^testBlock)(void) = ^{ NSLog(@"Integer is: %i", anInteger);};anInteger = 84;testBlock();// 輸出結果為:Integer is: 42// It also means that the block cannot change the value of the original variable// 在block defined的那一刻anInteger的值就已經被捕獲了
原理是如果聲名了__block,編譯器就會生成一段代碼將value從棧(stack)移到堆中(heap),同時編譯器會有一個強指針指向block中的每一個變量,確保它不會在block執行完畢前就離開堆,當執行完block的時候,編譯器將信息拷貝回堆當中,然后再移回棧當中,同時強指針會在block goes out of scope*1之前一直存在。
2.如果block不需要參數,參數的括號可以直接省去,如果返回值再return的時候可以直接看出來,比如return [MyClass isKindOfClass:...];,這種類型的編譯器會自動完成返回值,所以block的返回值類型也可以省去。
--------------------------------------------------------------- 動 畫 --------------------------------------------------------------------------------
1.使用animateWithDuration來執行動畫的時候如果動畫被中途中斷了,比如說將alpha從1.0設置為0,如果執行中途有代碼或者另一個動畫設置了alpha,則Completion的block的參數finished則為NO,如果沒有人中斷完成了動畫,則finished為YES。
2.UIViewAnimationOptions:
3.使用transitionWithView:(UIView *)view 方法來修改整個視圖的狀態。
duration:(NSTimeIntevel)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
4.transitionWithView的options:
5.transitionFromView:(UIView *)fromView 使用toView取代fromView。
toView:(UIView *)toView
duration:(NSTimeInteval)duration
options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion;
--------------------------------------------------------- DynamicAnimator -------------------------------------------------------------------------
1.創建動力動畫的步驟:
2.創建UIDynamicAnimator對象的時候直接使用alloc/init是可以的,但是如果作用于一個UIView對象則需要使用alloc/initWithReferenceView:方法來指定頂級視圖。UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:aView];
3.創建UIDynamicBehavior的時候直接使用alloc/init就可以了,然后將其加入UIDynamicAnimator中。UIGravityBehavior *gravity = [[UIGravity Behavior alloc] init];[animator addBehavior:gravity];
4.添加Item:id <UIDynamicItem> item1 = ...;[gravity addItem:item1];基本上90%的item都是UIView。
5.UIDynamicItem協議屬性方法:
6.如果要在animator執行動畫的時候修改item的屬性,必須要使用- (void)updateItemUsingCurrentState:(id <UIDynamicItem>)item;方法,不然animator會忽略所有其他的修改。
7.UIGravityBehavior(重力行為--控制Items的角度以及重力系數)屬性方法:
8.UICollisionBehavior(碰撞行為--行為中的Items碰撞在一起的時候會撞開,和現實中碰撞一樣,同時可以設置邊界,置頂彈力大小或者密度)屬性方法:
9.UIAttachmentBehavior(吸附行為--默認情況下吸附兩者之間的間距想一個鐵棒,通過對其阻尼大小(damping)和震蕩頻率(oscillation)進行設置可以使其變得想彈簧一樣)屬性方法:
10.UISnapBehavior(速甩行為我覺得這個翻譯實在是不好)屬性方法:
11.UIPushBehavior(推動行為)屬性方法:
12.UIDynamicItemBehavior(動力項行為--如果想要設置了例如摩擦力、彈力、密度等,不管進行什么行為,它的這些屬性應該是一樣的,他們都是在UIDynamicItemBehavior類中設置)屬性方法:
13.UIDynamicBehavior是所有行為的子類。可以使用- (void)addChildBehavior:(UIDynamicBehavior *)behavior;方法來添加子行為。
14.所有的behaviors都擁有一個屬性@property UIDynamicAnimator *dynamicAnimator;它們記錄的是所屬UIDynamicAnimator的信息。同時擁有一個方法- (void)willMoveToAnimator:(UIDynamicAnimator *)animator;,該方法會在behavior加入到animator或者移出的時候被調用,加入的時候參數是目標animator,移除的時候參數會被設置成nil。
15.UIDynamicBehavior的action property:@property (copy) void (^action)(void) 想要使用這個屬性,只要使用任意behavior加上.action并且賦值一個block,那么在每次執行behavior的時候都會進行調用該block。
--------------------------------------------------------------- 多 線 程 ----------------------------------------------------------------------------
1.線程可以理解為queue,queue中都是blocks。
2.MainQueue存儲的是需要改變屏幕顯示或者同步(讓一切改變和UI同步)等操作的block。
3.在另一個線程執行block:
4.獲得主要線程:
5.dispatch_queue_t otherQ = dispatch_queue_create("name", NULL); // 第一個是queue的名字,第二個則是指定queue是serial queue(串行隊列)或者concurrent queue(并發隊列),NULL代表是一個serial queue
6.將block放回主線程:
- (void)performSelectorOnMainThread:(SEL)aMethod
withObject:(id)obj // selector方法的參數,可以為nil
waitUntilDone:(BOOL)waitUntilDone; // is whether you're gonna wait until this thing gets pulled off the main queue and run on the main queue and then finishes before this thread, that's calling this, goes or not. (英文部分不是很理解),通常設置為NO,不需要等待,會直接再主隊列上調用這個方法, 輪到它執行的時候就執行
dispatch_async(dispatch_get_main_queue(), ^{ /* call aMethod */ })
7.使用多線程的例子:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@"http://..."]];
NSURLConfiguration *configuration = ...;
NSURLsession *session = ...;
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
/* want to do UI things here can I? */ /* must be main queue */
}];
[task resume];
8.創建Session的方法:
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSErr or *error) {
/ * yes, can do UI things directly because this is called on the main queue */
}];
[task resume];
9.沒有代理以及代理線程的創建session方法
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; // no delegateQueue
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
disapatch_async(dispatch_get_main_queue(), ^( /* do UI things */ ));
or [self performSelectorOnMainThread:@selector(doUIthings) withObject:nil waitUntilDone:NO];
}];
[task resume];
10.ScrollView獲得當前顯示區域在整個ScollView中的坐標
CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:subview];
11.- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated; // 對rect進行盡可能的方法或者縮小(顯示在屏幕上)
------------------------------------------------------------- 表格視圖和iPad --------------------------------------------------------------------------
突然感覺學的好像有點快了,開始回頭看以前知識的詳細解釋。
*1:英語不是很好,我的理解是block也是有作用域的,比如[UIView animateWithDuration:NSTimeInterval animations:^(void)animations]這個方法,再執行完該方法的時候可能block就已經失效了,block的作用與就是這一條語句,當然這是我的猜測,如有了解的請務必糾正我。
火狐的restart命令會讓博客園自動保存內容全部掛掉?明明自動保存了為什么重啟后讀取保存的幾個小時前的數據?寫了幾個小時就這么沒了,瞬間不想再寫博客了。
還是屈服了,從第4點開始可能記得有點亂,因為之前記了很多結果被該死的自動保存干掉了...
有點亂,分個類先
新聞熱點
疑難解答