這里只寫主要代碼,思路很簡單衔掸,看一下基本就明白了,RunLoop這東西平常不特意用它的話疲迂,用的地方很少若未。閑話不說朱嘴,上代碼。
#import "ViewController.h"
//定義一個block
typedef BOOL(^RunloopBlock)(void);
static NSString *IDENTIFIER = @"IDENTIFIER";
static CGFloat CELL_HEIGHT = 105.f;
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
/** 存放任務的數(shù)組 */
@property(nonatomic,strong)NSMutableArray * tasks;
/** 任務標記 */
@property(nonatomic,strong)NSMutableArray * tasksKeys;
/** 最大任務數(shù) */
@property(assign,nonatomic)NSUInteger max;
/** timer */
@property(nonatomic,strong)NSTimer * timer;
@property (nonatomic, strong) UITableView *exampleTableView;
@end
@implementation ViewController
-(void)_timerFiredMethod{
}
- (void)viewDidLoad {
[super viewDidLoad];
_max = 18;
_tasks = [NSMutableArray array];
_tasksKeys = [NSMutableArray array];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(_timerFiredMethod) userInfo:nil repeats:YES];
//注冊Cell
[self.exampleTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:IDENTIFIER];
//注冊監(jiān)聽
[self addRunloopObserver];
}
//MARK: 內部實現(xiàn)方法
//加載第一張
+(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初始化方法
//設置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é)約內存!!
for (NSInteger i = 1; i <= 3; i++) {
//干掉contentView 上面的所有子控件!!
[[cell.contentView viewWithTag:i] removeFromSuperview];
}
//不要直接加載圖片!! 你將加載圖片的代碼!都給RunLoop!!
[self addTask:^BOOL{
[ViewController addImage1With:cell];
return YES;
} withKey:indexPath];
[self addTask:^BOOL{
[ViewController addImage2With:cell];
return YES;
} withKey:indexPath];
[self addTask:^BOOL{
[ViewController addImage3With:cell];
return YES;
} withKey:indexPath];
return cell;
}
#pragma mark - <RunLoop>
//MARK: 添加任務
-(void)addTask:(RunloopBlock)unit withKey:(id)key{
[self.tasks addObject:unit];
[self.tasksKeys addObject:key];
//保證之前沒有顯示出來的任務,不再浪費時間加載
if (self.tasks.count > self.max) {
[self.tasks removeObjectAtIndex:0];
[self.tasksKeys removeObjectAtIndex:0];
}
}
//MARK: 回調函數(shù)
//定義一個回調函數(shù) 一次RunLoop來一次
static void Callback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
ViewController * vc = (__bridge ViewController *)(info);
if (vc.tasks.count == 0) {
return;
}
BOOL result = NO;
while (result == NO && vc.tasks.count) {
//取出任務
RunloopBlock unit = vc.tasks.firstObject;
//執(zhí)行任務
result = unit();
//干掉第一個任務
[vc.tasks removeObjectAtIndex:0];
//干掉標示
[vc.tasksKeys removeObjectAtIndex:0];
}
}
//這里面都是C語言 -- 添加一個監(jiān)聽者
-(void)addRunloopObserver{
//獲取當前的RunLoop
CFRunLoopRef runloop = CFRunLoopGetCurrent();
//定義一個centext
CFRunLoopObserverContext context = {
0,
( __bridge void *)(self),
&CFRetain,
&CFRelease,
NULL
};
//定義一個觀察者
static CFRunLoopObserverRef defaultModeObsever;
//創(chuàng)建觀察者
defaultModeObsever = CFRunLoopObserverCreate(NULL,
kCFRunLoopBeforeWaiting,
YES,
NSIntegerMax - 999,
&Callback,
&context
);
//添加當前RunLoop的觀察者
CFRunLoopAddObserver(runloop, defaultModeObsever, kCFRunLoopDefaultMode);
//c語言有creat 就需要release
CFRelease(defaultModeObsever);
}
@end