Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.47 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. UITextField keyboard blocks runloop while loading?
  2. @interface CustomTextField : UITextField {
  3.     UIActivityIndicatorView *spinner;
  4.     CGPoint spinnerCenter;
  5.     BOOL shouldShowSpinner;    
  6. }
  7. @property (assign) UIActivityIndicatorView *spinner;
  8. @property (assign) CGPoint spinnerCenter;
  9. @property (assign) BOOL shouldShowSpinner;
  10. - (void)stopSpinner;
  11. @end
  12.  
  13.  
  14. @implementation CustomTextField
  15.  
  16. @synthesize spinner;
  17. @synthesize spinnerCenter;
  18. @synthesize shouldShowSpinner;
  19.  
  20. - (id)initWithFrame:(CGRect)frame
  21. {
  22.     self = [super initWithFrame:frame];
  23.     if (self) {
  24.         self.spinnerCenter = self.frame.origin;
  25.         self.spinner = nil;
  26.         shouldShowSpinner = NO;
  27.         [[NSNotificationCenter defaultCenter] addObserver:self
  28.                                                  selector:@selector(stopSpinner)
  29.                                                      name:UIKeyboardDidShowNotification
  30.                                                    object:nil];
  31.     }
  32.     return self;
  33. }
  34.  
  35. - (void)stopSpinner
  36. {
  37.     shouldShowSpinner = NO;
  38.     if (spinner != nil) {
  39.         [spinner removeFromSuperview];
  40.         spinner = nil;
  41.     }
  42. }
  43.  
  44.  
  45. - (void)loadSpinner
  46. {
  47.     if (shouldShowSpinner) {
  48.         CGRect rect = CGRectMake(spinnerCenter.x - 15, spinnerCenter.y - 15, 30, 30);
  49.         self.spinner = [[[UIActivityIndicatorView alloc] initWithFrame:rect] autorelease];
  50.         [spinner startAnimating];
  51.         [theSuperview addSubview:spinner];
  52.     }
  53. }
  54.  
  55. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  56.     if ([self pointInside:point withEvent:event] && self.hidden == NO && self.isEditing == NO) {
  57.         shouldShowSpinner = YES;
  58.  
  59.         // the GCD approach, have to be stuck on current dispatch queue?
  60.         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 300000000), dispatch_get_current_queue(), ^{
  61.             if (shouldShowSpinner) {
  62.                 CGRect rect = CGRectMake(spinnerCenter.x - 15, spinnerCenter.y - 15, 30, 30);
  63.                 self.spinner = [[[UIActivityIndicatorView alloc] initWithFrame:rect] autorelease];
  64.                 [spinner startAnimating];
  65.                 [theSuperview addSubview:spinner];
  66.             }
  67.         });
  68.  
  69.         // The NSRunLoopApproach, NSRunLoopCommonModes blocked by keyboard loading?
  70.         [self performSelector:@selector(loadSpinner)
  71.                    withObject:nil
  72.                    afterDelay:0.3
  73.                       inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
  74.     }
  75.     return [super hitTest:point withEvent:event];
  76. }