2016-07-05 33 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; 

}