- (void)viewDidLoad
{
[super viewDidLoad];
self.imagePaths = [NSMutableArray array];
self.ivs = [NSMutableArray array];
// 与网页比较耗时,放在线程中操作
[NSThread detachNewThreadSelector:@selector(getImagePaths) toTarget:self withObject:Nil];
}
-(void)getImagePaths{
NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://beijing.bitauto.com"] encoding:NSUTF8StringEncoding error:Nil];
NSLog(@"%@",htmlString);
NSArray *arr= [htmlString componentsSeparatedByString:@"\""];
for (NSString *path in arr) {
if ([path hasSuffix:@"jpg"]) {
[self.imagePaths addObject:path];
}
}
// 取到到图片,回到主线程刷新界面
[self performSelectorOnMainThread:@selector(addImageView) withObject:Nil waitUntilDone:NO];
}
-(void)addImageView{
for (int i=0; i<9; i++) {
NSString *imagePath = self.imagePaths[i];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]];
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(80+i*10, 130, 80, 110)];
iv.image = [UIImage imageWithData:data];
iv.backgroundColor = [UIColor redColor];
iv.layer.borderWidth = 3;
iv.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:iv];
[self.ivs addObject:iv];
}
//开始移动图片 放在
[NSThread detachNewThreadSelector:@selector(timeAction) toTarget:self withObject:Nil];
}
-(void)timeAction{
NSLog(@"%@",self.view.subviews);
for (int i=0; i<9; i++) {
[NSThread sleepForTimeInterval:1];
// 刷新界面又要回到主线程
[self performSelectorOnMainThread:@selector(moveImageUI:) withObject:@(i) waitUntilDone:NO];
}
}
-(void)moveImageUI:(NSNumber *)number{
int i = number.intValue;
UIImageView *iv = self.ivs[8-i];
[UIView animateWithDuration:.8 animations:^{
// 三行三列分法
iv.frame = CGRectMake(i%3*self.view.bounds.size.width/3, i/3*self.view.bounds.size.height/3, self.view.bounds.size.width/3, self.view.bounds.size.height/3);
}];
}