block动画、排序、ASI请求(六)

续上一篇block开辟内存(五)

- (void)viewDidLoad
{
    [super viewDidLoad];
    _array = [[NSMutableArray alloc]initWithObjects:@"das",@"ab",@"bg",@"cf",@"hp",@"ipone",@"ios",@"mac",@"rp",@"zm",@"lf", nil];
}

- (IBAction)presentNewVC:(UIButton *)sender
{
    SecondVC *vc = [[SecondVC alloc]init];
    //completion 参数是模态弹出完成时调用的block
    [self presentViewController:vc animated:YES completion:
     ^{
         vc.view.backgroundColor = [UIColor yellowColor];
    }];
    [vc release];
}

- (IBAction)animationButtonClick:(UIButton *)sender
{
    NSLog(@"123");
    sender.frame = sender.frame = CGRectMake(26, 102, 268, 30);
    sender.backgroundColor = [UIColor blueColor];
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:
     ^{
         sender.frame = CGRectMake(26, 450, 268, 30);
         sender.backgroundColor = [UIColor redColor];
     } completion:^(BOOL finished)
     {
         if (finished)
         {
             NSLog(@"动画完成了");
         }
         else
         {
             NSLog(@"动画停止");
         }
     }];
    //停止动画
    [_animationButton.layer removeAllAnimations];
}

//排序
- (IBAction)sortClick:(UIButton *)sender
{
    [_array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        //比较数字
//        if ([obj1 integerValue] < [obj2 integerValue])
//        {
//            return NSOrderedAscending;
//        }
//        else
//        {
//            return NSOrderedDescending;
//        }
        //比较字符串
        return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
    }];
    NSLog(@"排序之后数组\n%@",_array);
}

//ASI请求
- (IBAction)ASIRequestClick:(UIButton *)sender
{
    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    //设置请求完成时的block
    [request setCompletionBlock:^
    {
        NSLog(@"%@",request.responseString);
    }];

    //设置请求失败时调用的block
    //[request setFailedBlock:<#^(void)aFailedBlock#>];
    [request startAsynchronous];
}
输出结果如下:

block动画、排序、ASI请求(六)_第1张图片

你可能感兴趣的:(block)