1

I'm trying to add a subview to view that displays an activity indicator and blocks touches while I'm fetching data from the server.

Here is the code that adds the loading view to the main view controller's screen:

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

Here is my loading view:

@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

I'm trying to make it so that the loading view absorbs/blocks all touches to other sibling views. It's working fine when I'm testing it by presenting it without doing any fetching, it enters the loading view's hitTest method and blocks the touch. However, when I'm actually doing any kind of fetch from the server, it doesn't seem to work. The touch is delayed a few seconds and does not get intercepted by the LoadingView.

I'm using GTMHTTPFetcher's

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

I'm not that sure what's going on and I tried looking for similar examples on SO, but couldn't find what I was looking for. Any help would be great, thanks!

Alex
  • 3,861
  • 5
  • 28
  • 44
  • Is your fetch being done on the main thread? Also, there is no need to implement hitTest, the view will catch touches by itself. Try removing `- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event` override. – Nicolas S Feb 09 '15 at 14:54
  • @Nicolas yeah, I thought GTM did everything on a different thread, but I guess not! Thanks – Alex Feb 09 '15 at 15:01

1 Answers1

0

I was doing my fetches on the main thread which was blocking the UI; I thought GTM did everything on a different thread, but it doesn't. Thanks to those who posted things which helped me in the right direction

Alex
  • 3,861
  • 5
  • 28
  • 44