2016-07-05 18 views
0

ich mehrere UITextField in einem Scrollview haben, und ich Textfelder Grenze Brauch vonUITextField Grenze Flashs

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 jedes Mal wenn ich konzentriert jede Textfield bekam Einstellung oder die Tastatur entlassen, die alle die Grenze blinkt das Textfeld. Aber wenn ich nur die scrollView scrollte keine blinkt

+0

warum Sie verwenden müssen '[UIView animateWithDuration: animDuration Animationen:^{ [se lf.scrollView scrollRectToVisible: rect animiert: NEIN]; }]; ', warum nicht verwenden' [self.scrollView scrollRectToVisible: rect animiert: NEIN]; 'direkt – childrenOurFuture

+0

@childrenOurFuture ist richtig, versuchen, ohne UiView Animation zu scrollen, aber mit animierten: YES – DoN1cK

+0

First Wenn die Tastatur ein- oder ausblenden Ich brauche den scrollView entsprechend mit der Tastaturanimationsdauer zu scrollen. –

Antwort

0

Ich finde endlich die Auflösung. Verwenden der Coreanimation rasteraztion, die shouldRasterize auf wahr Schicht des Textfield schalten: = true textField.layer.shouldRasterize und setzen den Maßstab. TextField.layer.rasterizationScale = UIScreen.mainScreen() -Skala shouldRasterize anweist CoreAnimatin die Schicht Inhalt zwischenzuspeichern als ein Bild. Sie werden dann das Blinken los. Um unnötiges Zwischenspeichern zu vermeiden, sollten Sie die Rasterung ausschalten, sobald die Animation fertig ist.

Der gesamte Code ist wie:

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

}