线程之NSThread二

- (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);

    }];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值