【问题标题】:UITextField's border flashsTextField 边框闪烁
【发布时间】:2016-11-06 21:01:24
【问题描述】:

我在滚动视图中有几个UITextField, 我通过设置自定义 textFields 的边框

layer.borderColor = ...,
layer.borderRadius = 3.0,
layer.borderWidth = 0.1,
layer.masksToBounds = YES. 

- (void)keyboardWillShow:(NSNotification *)notification {
CGFloat animDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

CGRect rect = [self.currentTextField superview].frame;

[UIView animateWithDuration:animDuration animations:^{
    [self.scrollView scrollRectToVisible:rect animated:NO];
}];
}

- (void)keyboardWillHide:(NSNotification *)notification {
CGFloat animDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect rect = [self.currentTextField superview].frame;
[UIView animateWithDuration:animDuration animations:^{        
    [self.scrollView scrollRectToVisible:rect animated:NO];
}];
}

每次我让任何文本字段聚焦或关闭键盘时,所有文本字段的边框都会闪烁。但是如果我只是滚动滚动视图就不会闪烁

【问题讨论】:

  • 为什么要使用[UIView animateWithDuration:animDuration animations:^{ [self.scrollView scrollRectToVisible:rect animated:NO]; }];,为什么不直接使用[self.scrollView scrollRectToVisible:rect animated:NO];
  • @childrenOurFuture 是对的,尝试在没有 UiView 动画的情况下滚动,但使用动画:YES
  • 首先当键盘显示或隐藏时,我需要滚动视图与键盘动画持续时间相应地滚动。
  • 第二次评论这两种方法,问题依旧

标签: ios uiscrollview uitextfield


【解决方案1】:

我终于找到了解决办法。 使用 CoreAnimation rasteraztion,将 textField 图层的 shouldRasterize 切换为 true: textField.layer.shouldRasterize = true 并设置比例: textField.layer.rasterizationScale = UIScreen.mainScreen().scale shouldRasterize 指示 CoreAnimatin 将图层内容缓存为图像。然后你摆脱闪烁。 为避免任何不必要的缓存,您应该在动画完成后立即关闭光栅化。

整个代码是这样的:

- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardRectEnd = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat animDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

CGRect rect = [self.currentTextField superview].frame;
self.scrollViewBottomConstraint.constant = keyboardRectEnd.size.height;

[self toggleTextFieldRasterization:YES];
[UIView animateWithDuration:animDuration animations:^{
    [self.scrollView scrollRectToVisible:rect animated:NO];
    [self layoutIfNeeded];
} completion: ^(BOOL finished) {
    [self toggleTextFieldRasterization:NO];
}];

}

- (void)keyboardWillHide:(NSNotification *)notification {
CGFloat animDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect rect = [self.currentTextField superview].frame;
self.scrollViewBottomConstraint.constant = 0.0;

[self toggleTextFieldRasterization:YES];
[UIView animateWithDuration:animDuration animations:^{
    [self.scrollView scrollRectToVisible:rect animated:NO];
    [self layoutIfNeeded];
} completion:^(BOOL finished) {
    [self toggleTextFieldRasterization:NO];
}];

}

- (void)toggleTextFieldRasterization:(BOOL)toggle {
CGFloat scale =  UIScreen.mainScreen.scale;
self.textField1.layer.shouldRasterize = toggle;
self.textField1.layer.rasterizationScale = scale;
self.textField2.layer.shouldRasterize = toggle;
self.textField2.layer.rasterizationScale = scale;
self.textField3.layer.shouldRasterize = toggle;
self.textField3.layer.rasterizationScale = scale;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2015-11-19
    相关资源
    最近更新 更多