2015-02-09 35 views
1

我想添加一个子视图来查看显示活动指示器并在从服务器获取数据时阻止触摸。提供阻止触摸的加载视图

这里是增加了加载视图到主视图控制器的屏幕代码:

+ (void)showLoadingView { 
    ViewController *vc = [AppDelegate mainViewController]; 
    if (!vc._activityViewContainer) { 
     LoadingView *loadingView = [[LoadingView alloc] initWithFrame:vc.view.bounds]; 
     [vc.view addSubview:loadingView]; 
     vc._activityViewContainer = loadingView; 
    } 
} 

+ (void)stopLoadingView { 
    ViewController *vc = [AppDelegate mainViewController]; 
    if (vc._activityViewContainer) { 
     [vc._activityViewContainer removeFromSuperview]; 
     vc._activityViewContainer = nil; 
    } 
} 

这里是我的加载视图:

@implementation LoadingView 

- (id)initWithFrame:(CGRect)frame {  
    if (self == [super initWithFrame:frame]) { 
     self.userInteractionEnabled = YES; 
     self.backgroundColor = [UIColor lightGrayColor]; 
     self.alpha = 0.6; 
     self.layer.zPosition = 10000; 
     UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
     NSUInteger avSize = 100; 
     CGRect activityViewFrame = CGRectMake((frame.size.width - avSize)/2, (frame.size.height - avSize)/2, avSize, avSize); 
     activityView.frame = activityViewFrame; 
     [self addSubview:activityView]; 
     [activityView startAnimating];  
    }  
    return self; 
} 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
    return [self pointInside:point withEvent:event] ? self : nil; } 
@end 

我试图让这个在加载视图吸收/阻挡所有触及其他兄弟视图。当我通过呈现它进行测试而没有进行任何提取时,它工作正常,它会进入加载视图的hitTest方法并阻止触摸。但是,当我实际上从服务器上进行任何类型的提取时,它似乎不起作用。触摸延迟了几秒钟,并且不会被加载视图拦截。

我使用GTMHTTPFetcher的

- (BOOL)beginFetchWithCompletionHandler:(void (^)(NSData *data, NSError *error))handler 

我没有那么肯定发生了什么事情,我试图寻找关于SO类似的例子,但无法找到我一直在寻找。任何帮助将是伟大的,谢谢!

+0

您的提取是在主线程上完成的吗?此外,没有必要实施hitTest,该视图将自己触及。尝试删除' - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event' override。 – 2015-02-09 14:54:20

+0

@Nicolas是的,我认为GTM做了不同的线程,但我猜不是!谢谢 – Alex 2015-02-09 15:01:44

回答

0

我当时正在阻塞UI的主线程上抓取数据;我认为GTM在一个不同的线程上做了所有事情,但事实并非如此。感谢那些发布的东西,帮助我在正确的方向