处理JSON时动画冻结

时间:2014-12-18 11:39:04

标签: ios objective-c multithreading animation

我用这种方法调用自定义动画:

- (void)spinWithOptions:(UIViewAnimationOptions)options directionForward:(BOOL)directionForward {

[UIView animateWithDuration:0.3
                      delay:0.0
                    options:options
                 animations:^{

                     CATransform3D transform;

                     if (!directionForward) {

                         transform = CATransform3DIdentity;

                     } else {

                         transform = CATransform3DIdentity;
                         transform.m34 = -1.0 / 500.0;
                         transform = CATransform3DRotate(transform, M_PI - 0.0001f /*full rotation*/, 0, 1, 0.0f);

                     }

                     self.logoImageView.layer.transform = transform;

                 } completion:^(BOOL finished) {

                     if (!_animating && options != UIViewAnimationOptionCurveEaseOut) {

                         [self spinWithOptions:UIViewAnimationOptionCurveEaseOut directionForward:NO];

                     } else if (options == UIViewAnimationOptionCurveLinear) {

                         [self spinWithOptions:UIViewAnimationOptionCurveLinear directionForward:!directionForward];

                     } else {

                         // animation finished

                     }

                 }];

}

但是我有一个问题,当动画运行并且我从AFNetworkingCoreData的服务器进行一些处理动画冻结时,我认为主线程已被阻止,但我也有一个{ {1}}并且没有冻结。知道我怎么能让这个动画不冻结?

1 个答案:

答案 0 :(得分:1)

您在同一个线程上执行动画和所有网络。只有一件事可以一次在一个线程上运行,所以你的加载会阻止动画。

你需要卸载任务,这样线程只能运行一件事。任何ui修改都需要在主线程上进行,因此我们卸载了网络。

- startMyLongMethod {
    [self startAnimation]; //your spin thingy

    //get a background thread from GCD and do the work there
    dispatch_async(dispatch_get_global_queue(0,0), ^{
        //do longRunning op (afnetwork and json parsing!)
        id result = [self doWork];

        //go back to the main thread and stop the animation
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateUI:result];
            [self stopAnimation];//your spin thingy
        });
    });

}

注意:示例代码和内联编写!现在应该清楚这种方法