Skip to content

Instantly share code, notes, and snippets.

View tailang's full-sized avatar
💭
I'm busy

tailang tailang

💭
I'm busy
View GitHub Profile
@tailang
tailang / Runloop添加Observer.md
Last active August 12, 2018 13:42
Runloop添加Observer.md
// 添加一个监听者
- (void)addObserver {
    
    // 1. 创建监听者
    /**
     *  创建监听者
     *
     *  @param allocator#>  分配存储空间
     *  @param activities#> 要监听的状态
@tailang
tailang / 在子线程跑一个timer.md
Last active October 12, 2017 10:50
在子线程跑一个timer
dispatch_async(dispatch_get_global_queue(0, 0), ^{
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        // NOTE: 子线程的runloop默认不创建; 在子线程获取 currentRunLoop 对象的时候,就会自动创建RunLoop
        // 这里不加到 main loop,必须创建一个 runloop
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [_link addToRunLoop:runloop forMode:NSRunLoopCommonModes];
        // 必须 timer addToRunLoop 后,再run
        [runloop run];
 });
@tailang
tailang / UITableViewAutomaticDimension.md
Created September 21, 2017 12:20
UITableViewAutomaticDimension height cahce
// this is the cell height cache; obviously you don't want a static size array in production
var itemHeights = [CGFloat](count: 1000, repeatedValue: UITableViewAutomaticDimension) 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  if itemHeights[indexPath.row] == UITableViewAutomaticDimension {
    itemHeights[indexPath.row] = cell.bounds.height
  }
  // fetch more cells, etc.
}
@tailang
tailang / 指定构造器.md
Created July 31, 2017 08:13
指定构造器
- (instancetype)initWithRequest:(NSURLRequest *)request
                        options:(YYWebImageOptions)options
                          cache:(nullable YYImageCache *)cache
                       cacheKey:(nullable NSString *)cacheKey
                       progress:(nullable YYWebImageProgressBlock)progress
                      transform:(nullable YYWebImageTransformBlock)transform
                     completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER;

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
//实现
/// Network thread entry point.
+ (void)_networkThreadMain:(id)object {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"com.xxx.xxx"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
 }
@tailang
tailang / TimerWithGCD.md
Created July 12, 2016 07:17
Creating a timer with Grand Central Dispatch

##Creating a timer with Grand Central Dispatch

At the following is the implementation file of a sample class that shows, how to make a timer with the help of Grand Central Dispatch. The timer fires on a global queue, just change the queue to the main queue or any custom queue and the timer fires on this queue and not on the global queue anymore.

#import <Foundation/Foundation.h>

@interface SampleClass : NSObject
- (void)startTimer;

NSOptation 是一个抽象类,主要使用NSOpration的两个子类NSBlockOperation, NSInvocationOperation

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
         NSLog(@"block 1 start");
        
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    button.frame = CGRectMake(0, 0, 50, 50);
    button.tag = 1001;
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
@tailang
tailang / UIViewController容器.md
Last active June 22, 2016 09:05
UIViewController容器Demo
//
//  MenuChartsViewContainerController.m
//  CardApp
//
//  Created by tailang on 6/22/16.
//  Copyright © 2016 2dfire.com. All rights reserved.
//