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

发布于 2025-01-07 00:21:24 字数 843 浏览 3 评论 0原文

触摸 UITextField 后,键盘大约需要 3-4 秒才会弹出。这只发生在应用程序启动后第一次弹出键盘时,之后动画立即开始。

起初我以为是加载太多图像的问题,或者是我的 UITableView 的问题,但我刚刚创建了一个只有 UITextField 的全新项目,我仍然遇到这个问题。我使用的是 iOS 5、Xcode 版本 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 秒再触摸文本字段,则不会出现延迟。

It takes roughly 3-4 seconds for the keyboard to pop up after I touch my UITextField. This only occurs on the first time the keyboard pops up since the app launched, afterwards the animation starts instantly.

At first I thought it was problem of loading too many images, or my UITableView, but I just created a brand new project with only a UITextField, and I still experience this problem. I'm using iOS 5, Xcode ver 4.2, and running on an iPhone 4S.

This is my code:

#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

Is this a common problem for all apps?

Right now, the only way I can make it somewhat better is by having textField become/resign first responder in viewDidAppear, but that doesn't solve the problem entirely - it just loads the delay onto when the view loads instead. If I click on textField immediately when the view loads, I still get the problem; if I wait 3-4 seconds after the view loads before touching the textField, I don't get the delay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷情 2025-01-14 00:21:24

在实施任何奇特的黑客方法来解决此问题之前,请尝试以下操作:停止调试会话,从多任务处理中关闭应用程序,从计算机上拔下设备,然后通过点击其图标正常运行应用程序。我至少见过两种情况,其中延迟仅在设备插入时发生。

Before you implement any exotic hacks to get around this problem, try this: stop the debug session, close the app from multitasking, unplug your device from the computer and run the app normally by tapping its icon. I have seen at least two cases in which the delay only occurs while the device is plugged in.

音盲 2025-01-14 00:21:24

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

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

So the problem is NOT just limited to the first install as I had previously thought, but happens every time the app is launched. Here's my solution that solves the issue completely.

- (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];
}
梦里寻她 2025-01-14 00:21:24

是的,我在最新的 iPhone 4s 上也遇到了几秒钟的延迟。不要恐慌。由于某些原因,只有在第一次从 Xcode 中以“调试”方式加载应用程序时才会发生这种情况。当我发布时,我没有得到延迟。就忘了吧...

Yeah, I also got a few seconds delay on the latest iPhone 4s. Don't panic. For some reasons, it only happens the first time the app is loaded from Xcode in Debug. When I did Release, I don't get the delay. Just forget it...

赤濁 2025-01-14 00:21:24

您可以在 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 中对我有用。

You can use Vadoff's solution in Swift by adding this to 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()

It is working for me in iOS 8.

玩物 2025-01-14 00:21:24

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

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

Code in block added to main queue and run asynchronously. (don't locked main thread)

dispatch_async(dispatch_get_main_queue(), ^(void){
      [textField becomeFirstResponder];
 });
回忆追雨的时光 2025-01-14 00:21:24

这个问题在 2024 年仍然与 Xcode 版本:15.3 和 Swift 版本:5.10 相关。

这个问题仅在调试时出现。关闭设备中的应用程序并手动重新打开它(而不是通过从 Xcode 运行)。这个问题得到解决。

This issue is still relevant in 2024 with Xcode version : 15.3 and Swift version : 5.10.

This issue only arises in debugging. Close the app in your device and re open it manually(not by running from Xcode). This issue gets resolved.

凉城凉梦凉人心 2025-01-14 00:21:24

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

This bug seems to be fixed in iOS 9.2.1. Since upgrading my device, I no longer have a delay between tapping a text field and the keyboard appearing when my device is connected to my computer.

半岛未凉 2025-01-14 00:21:24

您可以在 viewController 的视图加载时添加以下代码,例如 viewDidAppear.Not just application:didFinishLaunchingWithOptions:

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

You can add below code when viewController's view did loaded, like viewDidAppear.Not just application:didFinishLaunchingWithOptions:

UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文