super shen super shen - 5 months ago 20
iOS Question

UITextField's border flashs

I have several

UITextField
in a scrollView,
and I custom textFields' border by setting

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


Bug every time I got any textField focused or dismiss the keyboard, all the textfield's border flashes. But if I just scroll the scrollView no flashes

Answer

I finally find the resolution. Use the CoreAnimation rasteraztion, toggle the textField's layer's shouldRasterize to true: textField.layer.shouldRasterize = true and set the scale: textField.layer.rasterizationScale = UIScreen.mainScreen().scale shouldRasterize instructs CoreAnimatin to cache the layer contents as an image. You then get rid off the flashing. To avoid any unnecesary caching, you should turn off rasterization as soon as the animation is done.

The whole code is like:

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

}