iOS - How can I preload the keyboard?

萝らか妹 提交于 2019-12-20 08:29:59

问题


The Problem

In most iPhone apps, there's a quite a bit of delay the first time that the keyboard is presented (presumably creating the keyboard takes quite a bit of oomph, even on an iPhone 4).

Most people seem ok with this. I'm not, it really bugs me - and the way my app is presented, users will be very confused that nothing happens for a few seconds when they tap on a text field for the first time.


What I've Tried

Googling it brings up one solution - unfortunately this is invalid as of iOS 4 (see here).

I don't expect the solution to be easy to find, if I could put a bounty on this straight away I would. I would be very stoked if someone figured out a solution. All the solution needs to do is load the keyboard without the user being aware.


So..

Any ideas are appreciated. Complete, working code (for iOS 4 and 5) is bounty worthy (even if the bounty has to come later!).

If a solution is found I plan to create a self contained 'KeyboardPreloader' class that people can drop into their project, and preload the keyboard with one line of code :)


回答1:


UIResponder+KeyboardCache was written to address this exact problem.

From that project's readme:

This category on UIResponder gives you a simple method +cacheKeyboard so that you can control when this caching work is done. For example, if you are loading some data from a server, then you could invoke this during that downtime. There is another method +cacheKeyboard: that takes an optional BOOL value. Passing YES to this method causes the cache invocation to happen on the next runloop. So, if you performed an animation just before calling this method it would not interrupt that animation.




回答2:


making the textfield the firstResponder and then resigning it in the viewdidload.. this seems to work with no lag when the keyboard is loaded again...

- (void)viewDidLoad
{
    [super viewDidLoad];

    [textField becomeFirstResponder];
    [textField resignFirstResponder];
        // Do any additional setup after loading the view, typically from a nib.
}



回答3:


Here's what I do:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Preloads keyboard so there's no lag on initial keyboard appearance.
  UITextField *lagFreeField = [[UITextField alloc] init];
  [self.window addSubview:lagFreeField];
  [lagFreeField becomeFirstResponder];
  [lagFreeField resignFirstResponder];
  [lagFreeField removeFromSuperview];
}

Super slow lag/delay on initial keyboard animation of UITextField




回答4:


Once a user complain my app of the slow loading keyboard. Here is a little trick to disable the keyboard animation:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // ...

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willShowKeyboard:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didShowKeyboard:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    // ...
}


- (void)willShowKeyboard:(NSNotification *)notification
{
    [UIView setAnimationsEnabled:NO];
}

- (void)didShowKeyboard:(NSNotification *)notification
{
    [UIView setAnimationsEnabled:YES];
}

It may not answer the question directly, as the keyboard itself is in the main UI for my example and caching is not the option to me. Nevertheless, the overall responsiveness is improved.



来源:https://stackoverflow.com/questions/9108245/ios-how-can-i-preload-the-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!