处理 JSON 时动画冻结



我有一个自定义动画,我用这种方法调用:

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

}

但是我有一个问题,当动画运行时,我从具有AFNetworking的服务器进行一些处理,CoreData动画冻结,我认为主线程被阻塞了,但我也有MBProgresHUD,这不会冻结。知道我怎样才能使这个动画不冻结吗?

在同一线程上执行动画和所有网络。 但是,一次只能在一个线程上运行一件事,因此加载会阻止动画。

您需要卸载任务,以便线程只能运行一件事。任何 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
        });
    });

}

注意:示例代码和内联编写!不过,方法现在应该很清楚了

最新更新