代码运行后自定义segue动画和延迟。 (目标C)

时间:2012-11-24 21:10:20

标签: objective-c dispatch-async

我链接了几个dispatch_async方法,因此他们会对我系统中的用户进行身份验证。

下一个异步方法仅在前一个异步方法完成时发生,因为它们都有完成处理程序。

当最后一个完成时,我使用2个uiview动画块执行自定义segue。

但是,当我记录每个实际运行时,日志和实际发生的动画之间存在相当大的差距,最终会调用视图动画和完成块。

我真的不知道在这里添加我的代码有多么有用,但是我已经测试了它必须是异步方法,因为如果我将它们评论出来并且只返回YES动画就会立即发生与日志同时进行。

有谁知道为什么会这样?

编辑*(带代码)

用于电子邮件,用户,用户ID的典型“存在检查”。

- (void)existsInSystemWithCompletionHandler:(void (^)(BOOL))block
{
    self.existsInSystem = NO;

    if (self.isValid) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //
            //  Get data
            //
            if (dataIsValid) {
                block(YES);
            } else {
                block(NO);
            }
        });
    } else {
        block(self.existsInSystem);
    }
}

检查用户是否存在

[potentialUser existsInSystemWithCompletionHandler:^(BOOL success) {
    if (success) {
        //  Perform segue
        //
        [self performSegueWithIdentifier:@"Logging In" sender:self];
    }
}];

Segue公司

- (void)perform
{
    NSLog(@"Perform");

    LogInViewController *sourceViewController = (LogInViewController *)self.sourceViewController;
    LoggingInViewController *destinationViewController = (LoggingInViewController *)self.destinationViewController;

    destinationViewController.user = sourceViewController.potentialUser;

    //  Animate
    //
    [UIView animateWithDuration:0.2f
                     animations:^{
                         NSLog(@"Animation 1");
                         //
                         // Animate blah blah blah
                         //
                     }];

    [UIView animateWithDuration:0.4f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         NSLog(@"Animation 2");
                         //
                         // Animate blah blah blah
                         //
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"Completion");
                         //
                         // Finished
                         //

                         [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                     }];
}

LoggingIn VC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.user loginWithCompletionHandler:^(BOOL success) {
        if (success) {
            [self performSegueWithIdentifier:@"Logged In" sender:self];
        }
    }];
}

2 个答案:

答案 0 :(得分:1)

固定!它现在似乎工作,在我的代码中我添加了行:

dispatch_async(dispatch_get_main_queue(), ^{
    completionBlock(success);
});

答案 1 :(得分:0)

根据您的描述,您的segue似乎正在等待一个过程完成。

也许你的嵌套异步方法通过完成方法相互跟随,产生了一些复杂的代码。这可能是您可能忽略阻塞方法的原因。

整理代码的一种方法是使用顺序队列。推送到队列的块将仅在前一个块完成时启动。