成为FirstResponder非常慢

我有以下viewDidLoad方法:

- (void)viewDidLoad {
    NSLog(@"didLoad");
    if (self.loginField.text.length > 0) [self.passwordField becomeFirstResponder];
    else [self.loginField becomeFirstResponder];
}

我还添加日志时间viewWillAppearviewDidAppear

在某些情况下,推送动画会花费很多时间。我用带注释(和不带注释)的if-else行测量了时间(请参阅:时间显示如下)。我不知道是什么原因会减慢我的应用viewWillAppearviewDidAppear通话之间的速度

我试图使用Time Profiler(Instruments)来分析这段代码,但是什么也没显示。我不知道该怎么办才能更快地显示我的观点。有任何想法吗?

随着成为firstFirstResponder,第一个调用

2014-07-11 16:51:41.090 didLoad
2014-07-11 16:51:41.133 willAppear
2014-07-11 16:51:44.223 did appear
diffAppear = 3090ms

与成为firstFirstResponder,第二个电话

2014-07-11 16:52:01.370 didLoad
2014-07-11 16:52:01.400 willAppear
2014-07-11 16:52:02.109 did appear
diffAppear = 709ms

没有成为firstFirstResponder,就先打电话

2014-07-11 16:57:21.720 didLoad
2014-07-11 16:57:21.754 willAppear
2014-07-11 16:57:22.420 did appear
diffAppear = 666ms

没有成为firstResponder,第二个呼叫

2014-07-11 16:57:31.851 didLoad
2014-07-11 16:57:31.870 willAppear
2014-07-11 16:57:32.541 did appear
diffAppear = 671ms

作为@holex注释中的注释:

–becomeFirstResponser通常为需要时间的实际对象加载输入视图。另一方面,您应该在视图位于导航堆栈中并且在视图层次结构中正确之后调用此方法,这意味着在这种情况下:–viewDidAppear:方法中或之后,不要早于此。

我搬到–becomeFirstResponser-viewDidAppear:现在根据这个问题,我添加了以下观察者,如下所示,现在添加了应有的app。

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

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

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

这不是一个完美的解决方案(推送动画没有键盘),但对我来说已经足够了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章