UITextField键盘在加载时阻塞runloop?

因为我find了一个非常长的第一个加载时间的键盘(即使与iphone4S IOS 5)令人讨厌的问题,我试图实现一个自定义的UITextField显示一个UIActivityIndi​​cator如果键盘花费太长的时间来加载。 但是,我发现UIActivityIndi​​cator无法在键盘加载时添加到超级视图。 如果我设置了一个较短的延迟,或者指示器出现的方式太晚,就像键盘出现,当我设置一个稍长的延迟(0.3毫秒,尽pipe实际上需要1秒以上,如键盘加载)。

我已经尝试使用performSelector:withObject:afterDelay:inModes:在NSRunLoopCommonModes中设置我的活动指标,但似乎键盘加载阻止此模式。 我也尝试过使用GCD,但似乎遇到了与键盘加载和显示相同的调度队列中的活动指示器设置的相同问题。 如何在键盘加载时添加活动指示器视图? 这是我的代码:

@interface CustomTextField : UITextField { UIActivityIndicatorView *spinner; CGPoint spinnerCenter; BOOL shouldShowSpinner; } @property (assign) UIActivityIndicatorView *spinner; @property (assign) CGPoint spinnerCenter; @property (assign) BOOL shouldShowSpinner; - (void)stopSpinner; @end @implementation CustomTextField @synthesize spinner; @synthesize spinnerCenter; @synthesize shouldShowSpinner; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.spinnerCenter = self.frame.origin; self.spinner = nil; shouldShowSpinner = NO; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopSpinner) name:UIKeyboardDidShowNotification object:nil]; } return self; } - (void)stopSpinner { shouldShowSpinner = NO; if (spinner != nil) { [spinner removeFromSuperview]; spinner = nil; } } - (void)loadSpinner { if (shouldShowSpinner) { CGRect rect = CGRectMake(spinnerCenter.x - 15, spinnerCenter.y - 15, 30, 30); self.spinner = [[[UIActivityIndicatorView alloc] initWithFrame:rect] autorelease]; [spinner startAnimating]; [theSuperview addSubview:spinner]; } } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if ([self pointInside:point withEvent:event] && self.hidden == NO && self.isEditing == NO) { shouldShowSpinner = YES; // the GCD approach, have to be stuck on current dispatch queue? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 300000000), dispatch_get_current_queue(), ^{ if (shouldShowSpinner) { CGRect rect = CGRectMake(spinnerCenter.x - 15, spinnerCenter.y - 15, 30, 30); self.spinner = [[[UIActivityIndicatorView alloc] initWithFrame:rect] autorelease]; [spinner startAnimating]; [theSuperview addSubview:spinner]; } }); // The NSRunLoopApproach, NSRunLoopCommonModes blocked by keyboard loading? [self performSelector:@selector(loadSpinner) withObject:nil afterDelay:0.3 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; } return [super hitTest:point withEvent:event]; }