2016-07-05 35 views
0

我在一个滚动视图, 有几个UITextField并通过设定每一次我得到了集中的任何文本框或取消键盘时的UITextField的边界Flash的

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,该文本框的所有边境闪烁我的自定义文本框的边界。但如果我只是滚动scrollView不闪烁

+0

为什么你需要使用'[UIView的animateWithDuration:animDuration动画: [se lf.scrollView scrollRectToVisible:rect animated:NO]; }];',为什么不使用'[self.scrollView scrollRectToVisible:rect animated:NO];'直接 – childrenOurFuture

+0

@childrenOurFuture是正确的,尝试在没有UiView动画的情况下滚动,但是动画:YES – DoN1cK

+0

首先当键盘显示或隐藏,我需要使用键盘动画持续时间相应地滚动scrollView。 –

回答

0

我终于找到了决议。 使用CoreAnimation rasteraztion,切换TextField的层的shouldRasterize为true:textField.layer.shouldRasterize =真 并设置规模:textField.layer.rasterizationScale = UIScreen.mainScreen()规模 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; 

}