- UITextField keyboard blocks runloop while loading?
- @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];
- }