viewDidAppear用于子视图 [英] viewDidAppear for subviews

查看:124
本文介绍了viewDidAppear用于子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮时加载一些数据,并在加载时将加载视图"显示为当前视图的子视图.

I want to load some data when a button is pressed and show a "loading view" as a subview of my current view while it's loading.

所以我只想在子视图出现后才开始加载.如果不是这样,我的UI会卡住而不会发出通知(并且子视图仅在加载完成后显示).

So I want to start loading only after the subview did appear. If not my UI gets stuck without a notice (and the subview only shows after the loading is done).

是否可以使用诸如viewDidAppear之类的子视图?

Is there a way to use something like viewDidAppearfor subviews?

像这样在addSubview:之后立即进行工作不起作用:

Doing the work right after the addSubview: like this doesn't work:

- (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,这不是线程安全的.)

(I don't want to do the loading on a new thread, because is's CoreData i'm working on, which is't thread safe).

谢谢!

推荐答案

我找到了解决方法:

为子视图添加动画,我可以使用- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag来调用子视图的委托的subviewDidAppear之类的东西.

Adding the subview with an animation I could use - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag to call something like subviewDidAppear of the delegate of the subview.

在UIView的子类中:

In subclass of 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];
    }
}

这给了我我想要的结果.

This got me the results I was looking for.

再次感谢您的帮助!

这篇关于viewDidAppear用于子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆