我在scrollView中有几个UITextField
,
我通过设置
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];
}];
}
每次我有任何textField聚焦或关闭键盘时,所有文本字段的边框都会闪烁。但是,如果我只是滚动滚动浏览没有闪烁
答案 0 :(得分:0)
我终于找到了解决方案。 使用CoreAnimation rasteraztion,将textField的图层的shouldRasterize切换为true:textField.layer.shouldRasterize = true 并设置比例:textField.layer.rasterizationScale = UIScreen.mainScreen()。scale shouldRasterize指示CoreAnimatin将图层内容缓存为图像。然后你摆脱了闪烁。 为避免任何不必要的缓存,您应该在动画完成后立即关闭光栅化。
整个代码如下:
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
}
- (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];
}];
}