3

ボタンが押されたときにデータをロードし、ロード中に現在のビューのサブビューとして「ロードビュー」を表示したいと思います。

そのため、サブビューが表示された後でのみロードを開始したいと思います。そうでない場合、UIが通知なしにスタックします(サブビューはロードが完了した後にのみ表示されます)。

viewDidAppearサブビューのようなものを使用する方法はありますか?

このような直後に作業を行うと、機能addSubview:しません。

- (void)doSomeWorkAndShowLoadingView
{
    UIView *loadingView = [[[UIView alloc] initWithFrame:self.view.frame] autorelease];
    loadingView.backgroundColor = [UIColor redColor];
    [self.view addSubview:loadingView];
    [self doSomeWork];
    [loadingView removeFromSuperview];
}
- (void)doSomeWork
{ 
    sleep(5);
}

(私が取り組んでいるのはCoreDataであり、スレッドセーフではないため、新しいスレッドでロードを実行したくありません)。

ありがとう!

4

2 に答える 2

2

私は解決策を見つけました:

アニメーションでサブビューを追加すると、サブビューのデリゲートの- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flagようなものを呼び出すために使用できます。subviewDidAppear

UIViewのサブクラス:

#define KEY_SHOW_VIEW @"_ShowLoadingView_"
#define KEY_HIDE_VIEW @"_HideLoadingView_"
- (void)addToSuperview:(UIView *)theSuperview
{

    [theSuperview addSubview:self];   

    CATransition *animation = [CATransition animation];
    [animation setDuration:0.2];
    [animation setType:kCATransitionFade];
    [animation setDelegate:self];
    [animation setRemovedOnCompletion:NO];
    [animation setValue:KEY_SHOW_VIEW forKey:@"animation_key"];
    [[theSuperview layer] addAnimation:animation forKey:nil];

}

- (void)removeFromSuperview
{
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.2];
    [animation setType:kCATransitionFade];
    [animation setDelegate:self];
    [animation setRemovedOnCompletion:NO];
    [animation setValue:KEY_HIDE_VIEW forKey:@"animation_key"];
    [[self.superview layer] addAnimation:animation forKey:nil]; 

    [super removeFromSuperview];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{    
    NSString* key = [anim valueForKey:@"animation_key"];
    if ([key isEqualToString:KEY_SHOW_VIEW]) {
        if (self.delegate) {
            if ([self.delegate respondsToSelector:@selector(loadingViewDidAppear:)]) {
                [self.delegate loadingViewDidAppear:self];
            } 
        }
    } else if ([key isEqualToString:KEY_HIDE_VIEW]){
        [self removeFromSuperview];
    }
}

これは私が探していた結果を得ました。

助けてくれてありがとう!

于 2011-03-31T22:08:02.983 に答える
1

呼び出しの直後にロードを開始する[parentView addSubview:loadingView]か、ローディング ビュー (サブクラス化されていると仮定) で次のようにdidMoveToSuperviewをオーバーライドすることができます。

- (void)didMoveToSuperview {
    // [self superview] has changed, start loading now...
}
于 2011-03-29T09:50:23.600 に答える