how I can run my code one piece at a time. That is, I have a code that you want to perform consistently. Example:
- (IBAction)downloadPressed:(id)sender {
//1
CGRect frameDelete = deleteButton.frame;
frameDelete.origin.y = (deleteButton.frame.origin.y-50);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.5];
deleteButton.frame = frameDelete;
[UIView commitAnimations];
[progressLine setProgress:0];
//2 Wait until the code is executed above, and then run the code below
NSURL *fileURL = [NSURL URLWithString:@"http://intercreate.ru/all.zip"];
[self downloadDataAtURL:fileURL];
//3 Wait until the code is executed above, and then run the code below
CGRect frameDelete1 = deleteButton.frame;
frameDelete1.origin.y = (deleteButton.frame.origin.y+50);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.5];
deleteButton.frame = frameDelete1;
}
That is, I want my code was divided into three parts:
How do I do this?
The traditional way to do things like this is protocol/delegate design pattern. This is, however, obsolete because of blocks. They are the easiest way to accomplish this. They are, for example built in to the excellent MBProgressHUD. You can accomplish what you want with this:
- (IBAction)downloadPressed:(id)sender {
CGRect frameDelete = deleteButton.frame;
frameDelete.origin.y = (deleteButton.frame.origin.y-50);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.5];
deleteButton.frame = frameDelete;
[UIView commitAnimations];
[progressLine setProgress:0];
MBProgressHUD *aHud = [[MBProgressHUD alloc] initWithView:self.view];
[aHud showHudAnimated: NO whileExecutingBlock:^
{
//2 Wait until the code is executed above, and then run the code below
NSURL *fileURL = [NSURL URLWithString:@"http://intercreate.ru/all.zip"];
[self downloadDataAtURL:fileURL];
} completionBlock: ^
{
//3 Wait until the code is executed above, and then run the code below
CGRect frameDelete1 = deleteButton.frame;
frameDelete1.origin.y = (deleteButton.frame.origin.y+50);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.5];
deleteButton.frame = frameDelete1;
[UIView commitAnimations];
}]; }
Progress HUD can do much more, e.g. Show progress indicators :)
Check out this post for blocks usage: link