Runloop應(yīng)用

/*
分析:卡頓的原因!!
1.渲染圖片耗時(shí)!! -- 分段!!
每次Runloop循環(huán),最多需要加載18張大圖!! 所以卡住了!!
思路:
每次Runloop循環(huán),只渲染一張大圖!!
步驟:
1.監(jiān)聽(tīng)Runloop的循環(huán)!!
2.將加載大圖的代碼!放在一個(gè)數(shù)組里面!!
3.每次Runloop循環(huán),取出一個(gè)加載大圖的任務(wù)執(zhí)行!!
*/

import "ViewController.h"

typedef void(^runloopBlock)(void);

static NSString * IDENTIFIER = @"IDENTIFIER";
static CGFloat CELL_HEIGHT = 135.f;

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *exampleTableView;
@property(nonatomic,strong)NSMutableArray * tasks;

@end

@implementation ViewController

-(void)timerMethod{
//不干任何事情!
}

  • (void)viewDidLoad {
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];

_tasks = [NSMutableArray array];
         //注冊(cè)Cell
[self.exampleTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:IDENTIFIER];

[self addRunloopObserver];

}

//MARK: 內(nèi)部實(shí)現(xiàn)方法

//添加文字
+(void)addlabel:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 300, 25)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%zd - Drawing index is top priority", indexPath.row];
label.font = [UIFont boldSystemFontOfSize:13];
label.tag = 4;
[cell.contentView addSubview:label];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 99, 300, 35)];
label1.lineBreakMode = NSLineBreakByWordWrapping;
label1.numberOfLines = 0;
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor colorWithRed:0 green:100.f/255.f blue:0 alpha:1];
label1.text = [NSString stringWithFormat:@"%zd - Drawing large image is low priority. Should be distributed into different run loop passes.", indexPath.row];
label1.font = [UIFont boldSystemFontOfSize:13];
label1.tag = 5;
[cell.contentView addSubview:label1];

}

//加載第一張
+(void)addImage1With:(UITableViewCell *)cell{
//第一張
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 20, 85, 85)];
imageView.tag = 1;
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path1];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
[cell.contentView addSubview:imageView];
} completion:nil];
}

//加載第二張
+(void)addImage2With:(UITableViewCell *)cell{
//第二張
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(105, 20, 85, 85)];
imageView1.tag = 2;
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:path1];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
imageView1.image = image1;
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
[cell.contentView addSubview:imageView1];
} completion:nil];
}
//加載第三張
+(void)addImage3With:(UITableViewCell *)cell{
//第三張
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(200, 20, 85, 85)];
imageView2.tag = 3;
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
UIImage *image2 = [UIImage imageWithContentsOfFile:path1];
imageView2.contentMode = UIViewContentModeScaleAspectFit;
imageView2.image = image2;
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
[cell.contentView addSubview:imageView2];
} completion:nil];
}

//MARK: UI初始化方法
//設(shè)置tableview大小

  • (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.exampleTableView.frame = self.view.bounds;
    }

//Cell 高度

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CELL_HEIGHT;
    }

//加載tableview

  • (void)loadView {
    self.view = [UIView new];
    self.exampleTableView = [UITableView new];
    self.exampleTableView.delegate = self;
    self.exampleTableView.dataSource = self;
    [self.view addSubview:self.exampleTableView];
    }

pragma mark - <tableview>

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 399;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//干掉contentView上面的子控件!! 節(jié)約內(nèi)存!!
for (NSInteger i = 1; i <= 5; i++) {
    //干掉contentView 上面的所有子控件!!
    [[cell.contentView viewWithTag:i] removeFromSuperview];
}
//添加文字
[ViewController addlabel:cell indexPath:indexPath];
//添加圖片
[self addTasks:^{
    [ViewController addImage1With:cell];
}];

[self addTasks:^{
    [ViewController addImage2With:cell];
}];

[self addTasks:^{
    [ViewController addImage3With:cell];
}];


return cell;

}

pragma mark - <CFRunloop>

-(void)addTasks:(runloopBlock)task{

[self.tasks addObject:task];
if (self.tasks.count > 18) {
    [self.tasks removeObjectAtIndex:0];
}

}

//一下都是C語(yǔ)言的!!
-(void)addRunloopObserver{
//獲取Runloop
CFRunLoopRef runloop = CFRunLoopGetCurrent();
//定義一個(gè)context
CFRunLoopObserverContext context = {
0,
(__bridge void *)(self),
&CFRetain,
&CFRelease,
NULL
};

//定義觀察者
static CFRunLoopObserverRef runloopObserver;
runloopObserver = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting, YES, 0, &callBack, &context);

//添加觀察者
CFRunLoopAddObserver(runloop, runloopObserver, kCFRunLoopCommonModes);


//C里面 一旦creat new copy
CFRelease(runloopObserver);

}

void callBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
ViewController * vc = (__bridge ViewController *)info;
if(vc.tasks.count == 0){
return;
}
runloopBlock block = vc.tasks.firstObject;
block();
[vc.tasks removeObjectAtIndex:0];

}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌譬胎,老刑警劉巖凶赁,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件央星,死亡現(xiàn)場(chǎng)離奇詭異雷袋,居然都是意外死亡梆惯,警方通過(guò)查閱死者的電腦和手機(jī)备韧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門劫樟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事毅哗√拢” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵虑绵,是天一觀的道長(zhǎng)尿瞭。 經(jīng)常有香客問(wèn)我,道長(zhǎng)翅睛,這世上最難降的妖魔是什么声搁? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮捕发,結(jié)果婚禮上疏旨,老公的妹妹穿的比我還像新娘。我一直安慰自己扎酷,他們只是感情好檐涝,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著法挨,像睡著了一般谁榜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凡纳,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天窃植,我揣著相機(jī)與錄音,去河邊找鬼荐糜。 笑死巷怜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的暴氏。 我是一名探鬼主播延塑,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼答渔!你這毒婦竟也來(lái)了页畦?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤研儒,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后独令,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體端朵,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年燃箭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冲呢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡招狸,死狀恐怖敬拓,靈堂內(nèi)的尸體忽然破棺而出邻薯,到底是詐尸還是另有隱情,我是刑警寧澤乘凸,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布厕诡,位于F島的核電站,受9級(jí)特大地震影響营勤,放射性物質(zhì)發(fā)生泄漏灵嫌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一葛作、第九天 我趴在偏房一處隱蔽的房頂上張望寿羞。 院中可真熱鬧,春花似錦赂蠢、人聲如沸绪穆。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)玖院。三九已至,卻和暖如春量瓜,著一層夾襖步出監(jiān)牢的瞬間司恳,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工绍傲, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留扔傅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓烫饼,卻偏偏與公主長(zhǎng)得像猎塞,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子杠纵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容