【问题标题】:queue in Objective-cObjective-c 中的队列
【发布时间】:2012-12-16 11:03:59
【问题描述】:

如何一次运行我的代码。也就是说,我有一个您想要始终如一地执行的代码。示例:

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

}

也就是说,我要把我的代码分成三部分:

  1. 先将旋钮向上移动
  2. 下载文件
  3. 后退按钮。

我该怎么做?

【问题讨论】:

    标签: objective-c xcode queue dispatch


    【解决方案1】:

    执行此类操作的传统方式是协议/委托设计模式。但是,由于块,这已经过时了。它们是实现这一目标的最简单方法。例如,它们内置于出色的MBProgressHUD。你可以用这个来完成你想要的:

    - (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 可以做的更多,例如显示进度指示器:)

    查看这篇文章了解块的使用情况:link

    【讨论】:

      【解决方案2】:

      前半部分可以使用较新的基于块的动画,您可以提供一段在动画完成时执行的代码。第二部分我肯定会使用通知,您可以在文件下载完成时发布通知,然后您的代码的任何其他部分可以以他们想要的任何方式响应此事件,更改 GUI,通知用户,删除要下载的内容列表,您还可以添加新内容来收听此通知,而无需更改原始代码。

      【讨论】:

        【解决方案3】:

        您似乎在使用旧式 beginAnimations commitAnimations 方法。

        如果您改用新的基于块的动画方法,您将获得能够使用完成处理程序块的优势,并且如果您使用异步 URL 下载方法,API 还提供了一个添加完成块的地方。

        所以,基本上:

        1. 创建一个基于块的动画来移动按钮
        2. 在此动画的完成处理程序中触发异步下载
        3. 在下载的完成处理程序中触发动画以移动按钮。

        因为这些都在一个基于块的方法(原始动画块)中,所以各个动作将一个接一个地发生。

        【讨论】:

          猜你喜欢
          • 2012-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-30
          • 1970-01-01
          • 2013-03-13
          • 2010-10-23
          相关资源
          最近更新 更多