2

ビューを非同期で読み込もうとしています。問題は、読み込まれるビューのフレームが、非同期で読み込まれるデータに依存することです。つまり、UIView を実際に表示する場所を決定する長い計算がいくつかあります。

スレッドで UIView を実際に表示しようとすると問題が発生し、常にメイン スレッドにロードする必要があることを知っているので、これは私が試したコードです。

asyncQueue = [[NSOperationQueue alloc] init];
[asyncQueue addOperationWithBlock:^{
    // Do work to load the UIViews and figure out where they should be
    UIButton *test = [[UIButton alloc] initWithFrame:[self doWorkToGetFrame]];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self addSubview:test];
        }
    }];
}];

これはすべて UIView コンテナにあります。

4

1 に答える 1

3

次のようなことを試してください:

UIButton *test = [[UIButton alloc] initWithFrame:CGRectZero];
test.hidden = YES;
test.alpha = 0.0f;
[self addSubview:test];

dispatch_queue_t downloadQueue = dispatch_queue_create("myDownloadQueue",NULL);
dispatch_async(downloadQueue, ^
{
  // do work to load UIViews and calculate frame
  CGRect frameButton = ...;

  dispatch_async(dispatch_get_main_queue(), ^
  {
    test.hidden = NO; 
    [UIView animateWithDuration:0.4f animations:^
    {
     test.frame = frameButton;
     test.alpha = 1.0f;
    } 
    completion:^(BOOL finished){}];


  });
});

dispatch_release(downloadQueue);

これにより、メイン スレッドにボタンが追加されますが、最初は非表示になります。背景の作業が完了したら、それを表示し、アニメーションを使用してフレームを設定します。

于 2012-09-11T16:48:09.473 に答える