ios UITextField初始键盘动画的超慢滞后/延迟

siotufzp  于 2023-05-30  发布在  iOS
关注(0)|答案(7)|浏览(225)

在我触摸我的UITextField后,键盘大约需要3-4秒钟才会弹出。这只发生在应用程序启动后键盘第一次弹出时,之后动画立即开始。
起初我以为是加载太多图像的问题,或者我的UITableView,但我只是创建了一个只有UITextField的全新项目,我仍然遇到这个问题。我正在使用iOS 5,Xcode ver 4.2,并在iPhone 4S上运行。
这是我的代码:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.delegate = self;
    [self.view addSubview:textField];
}

@end

这是所有应用程序的常见问题吗?
现在,我可以使它稍微好一点的唯一方法是让textField成为viewDidAppear中的第一响应者,但这并不能完全解决问题-它只是在视图加载时加载延迟。如果在视图加载时立即单击textField,仍然会出现问题;如果我在视图加载后等待3-4秒,然后再触摸textField,我不会得到延迟。

enxuqcxy

enxuqcxy1#

在你实施任何外来的黑客来解决这个问题之前,试试这个:停止调试会话,关闭应用程序的多任务处理,从计算机上拔下您的设备,并通过点击其图标正常运行应用程序。我至少见过两种情况,延迟只在设备插上电源时发生。

8cdiaqws

8cdiaqws2#

所以问题并不像我之前想的那样只限于第一次安装,而是每次启动应用程序时都会发生。这是我的解决方案,完全解决了这个问题。

- (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];
}
fcwjkofz

fcwjkofz3#

是的,我在最新的iPhone 4s上也有几秒钟的延迟。别慌由于某些原因,它只发生在第一次从Debug中的Xcode加载应用程序时。当我释放的时候,我没有得到延迟。算了吧...

fykwrbwg

fykwrbwg4#

你可以在Swift中使用Vadoff的解决方案,方法是在didFinishLaunchingWithOptions中添加:

// Preloads keyboard so there's no lag on initial keyboard appearance.
let lagFreeField: UITextField = UITextField()
self.window?.addSubview(lagFreeField)
lagFreeField.becomeFirstResponder()
lagFreeField.resignFirstResponder()
lagFreeField.removeFromSuperview()

它在iOS 8中为我工作。

ttp71kqs

ttp71kqs5#

块中的代码添加到主队列并异步运行。(不锁定主线程)

dispatch_async(dispatch_get_main_queue(), ^(void){
      [textField becomeFirstResponder];
 });
rqenqsqc

rqenqsqc6#

这个bug似乎在iOS 9.2.1中得到了修复。自从升级了我的设备,我不再有点击文本字段和键盘出现之间的延迟,当我的设备连接到我的电脑。

mu0hgdu0

mu0hgdu07#

你可以在viewController的视图加载时添加下面的代码,比如viewDidAppear。不仅仅是application:didFinishLaunchingWithOptions:

UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];

相关问题