解決Scroll View Auto Layout的warring issue,
原則上Scroll View裡面只能放一個View,
故拉一個UIView等同scroll view的寬&高
Frost's Blog
How about a magic trick?
2015-12-21
2015-11-21
(ios)move TableView cell
http://www.raywenderlich.com/63089/cookbook-moving-table-view-cells-with-a-long-press-gesture
http://www.cnblogs.com/iCocos/p/4674721.html
http://stackoverflow.com/questions/6097464/reordering-controls-on-uitableview-while-not-in-editing-mode
http://www.cnblogs.com/iCocos/p/4674721.html
http://stackoverflow.com/questions/6097464/reordering-controls-on-uitableview-while-not-in-editing-mode
標籤:
iOS
2015-11-03
[iOS] NSOperation & NSOperation Queue
NSOperation:
1. NSInvocationOperation
ex:
NSInvocationOperation *invacationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomethingWithObj:) object:obj];
get result:
id result = [invacationOperation result];
2. NSBlockOperation
ex:
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:
^{
//do something
}];
3. 自定義(繼承)NSOperation
- NSInvocationOperation & NSBlockOperation 兩者實際上無太大差別
- Start & Cancel 一個 Operation
[operation start];
[operation cancel];
- 設置Operation的completionBlock:
completionBlock實現原理是對Operation的isFinnshed字段進行KVO(Key-Value Observing),當監聽到isFinnished變成YES时,就执行completionBlock。
operation.completionBlock =
^{
NSLog(@"finished");
};
NSOperationQueue:
NSOperationQueue是一个Operation执行队列,你可以将任何你想要执行的Operation添加到Operation Queue中,以在队列中执行。'
- Operation的依賴關係:
[operation addDependency:anotherOperation];
[operation removeDependency:anotherOperation];
如果将这些Operation和它所依赖的Operation加如队列中,那么Operation只有在它依赖的Operation都执行完毕后才可以被执行。
- Operation在队列中执行的优先级:
Operation在队列中默认是按FIFO(First In First Out)顺序执行的。同时我们可以为单个的Operation设置一个执行的优先级,打乱这个顺序。
- 最大並行Operation數目:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
* 實驗結果:區域變數中的NSBlockOperation 須先經過init, 才能在其他function中被cancel.
ex:
NSBlockOperation *op2 = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOperation = op2;
[op2 addExecutionBlock:
^{
//while (true)
for(int i = 0; i<30; i++)
{
NSLog(@"op2 %@", op2);
if ([op2 isCancelled])
{
return;
}
[NSThread sleepForTimeInterval:1];
}
}];
標籤:
iOS
2015-10-29
[iOS] GCD - dispatch
Queues Types:
- Serial Queues
Tasks in serial queues execute one at a time, each task starting only after the preceding task has finished.
- Concurrent Queues
Tasks in concurrent queues are guaranteed to start in the order they were added,
Items can finish in any order and you have no knowledge of the time it will take for the next block to start, nor the number of blocks that are running at any given time.
- Main Queues
Items can finish in any order and you have no knowledge of the time it will take for the next block to start, nor the number of blocks that are running at any given time.
- Main Queues
Like any serial queue, tasks in this queue execute one at a time. However, it’s guaranteed that all tasks will execute on the main thread, which is the only thread allowed to update your UI. This queue is the one to use for sending messages to
- Examples:
UIViews
or posting notifications.- Examples:
// background執行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
// main thread執行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
});
// 一次性執行:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// code to be executed once
});
// delay 2 秒執行:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on the main queue after delay
});
// 自定義Queue (Serial Queue)
dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
dispatch_async(urls_queue, ^{
// your code
});
dispatch_release(urls_queue);
- dispatch_group:
GCD 还有一些高级用法,例如让后台 2 个线程并行执行,然后等 2 个线程都结束后,再汇总执行结果。这个可以用 dispatch_group, dispatch_group_async 和 dispatch_group_notify 来实现,示例如下:
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 並行執行的 Thread 1
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 並行執行的 Thread 2
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// thread 1 & 2 完成後執行
});
標籤:
iOS
[iOS] Auto Layout - NSLayoutConstraint
Object - C:
NSMutableArray *myConstraints = [NSMutableArray array];
NSDictionary *variableBindings = @{@"_btn_cancel" : _btn_cancel};
NSDictionary *metrics = @{@"height": 100,
@"margin": 10};
// ex1: 從水平方向佈局
[myConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|-margin-[_btn_cancel(height)]-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:metrics
views:variableBindings]];
//ex2: 從垂直方向佈局
[myConstraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:
@"V:[ViewB(ViewA)]-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(ViewB,ViewA)]];
[self.view addConstraints:myConstraints];
參數說明:
options: 佈局時的順序,預設是由上而下(垂直)與由左而右(水平)
metrics : 一般为nil ,参数类型为NSDictionary, 為要傳入的變數
views :参数类型为NSDictionary, 為要傳入的Views
metrics : 一般为nil ,参数类型为NSDictionary, 為要傳入的變數
views :参数类型为NSDictionary, 為要傳入的Views
語法:
* " V: " 和 " H: "
分別代表由垂直或是水平方向來佈局。
*" | "
代表 Superview 的邊界。
*" - "
代表預設距離,例如在中間加上數字 " -20- ",則代表限制 20 個單位距離。
*" [ ] "
代表物件本身,括號內包含物件的變數名稱與大小限制,可以使用關係運算子(<=、>= 或 == 等)。
* " @ "
優先權,1 至 1000 的整數,優先權較大的條件會優先被滿足,例如 ,[ViewB(>=100@1000)],物件 ViewB 不可以小於 100 個單位長度或寬度會最優先被考慮。
標籤:
iOS
訂閱:
文章 (Atom)