2013-01-10 6 views
0

텍스트 필드를 누르면 키보드가 숨겨집니다. 그래서이 코드를 구현하여 뷰를 "스크롤"합니다.다른보기에 대한 키보드 숨기기 텍스트 필드

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: NO]; 
} 

- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
{ 
    const int movementDistance = 80; // tweak as needed 
    const float movementDuration = 0.3f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 

이 작업을 수행하려고하는 UIView는 다른 펜촉입니다. 나는 두 개의 UIView를 가지고 있기 때문에 프로그램이 혼란스러워지고 애니메이션을 설정할 대상을 모른다.

오류는 발생하지 않지만 애니메이션은 발생하지 않습니다.

나는 애니메이션을 적용하려고하는 뷰에 대해 UIView "surveyPage"를 선언했습니다. 위의 코드에서 애니메이션을 적용 할 UIView를 지정해야하는 곳이 있습니까?

업데이트 :

제안은 아래 나를 위해 작동하지 않았다. 나는 self.view를 surveyPage로 바꾸려고 노력했다. 제가 위에서 게시 된 코드 위

, 나는 내 프로그램이 있습니다

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.view endEditing:YES]; 
} 

위의 코드는 문제없이 작동, 아직 애니메이션은하지 않습니다.

+0

나는 비슷한 작업을 하였다

[UIView animateWithDuration:movementDuration animations:^{ self.view.frame = CGRectOffset(self.view.frame, 0, movement); }]; 

, 어쩌면 코드의 일부는 여기 http://stackoverflow.com/questions/11722732 도움이 될 것입니다/keyboard-hiding-uiview-ios –

+0

링크를 주셔서 감사하지만 도움이되지 않았다 : ( – Adam

+0

@ Adam : 안녕하세요 .. 내가 문제를 해결 했나요? 내 텍스트 필드를 제외하고 같은 문제가 생겼어요 대신 서브 뷰 안에 있습니다. 기본보기. –

답변

0

당신은 여기에 UIView를 지정하고 있습니다 :

self.view.frame = CGRectOffset(self.view.frame, 0, movement); 

self.view을합니다.

시도 : 잠시 뒤로 대신

[UIView beginAnimations: @"anim" context: nil]; 
[UIView setAnimationBeginsFromCurrentState: YES]; 
[UIView setAnimationDuration: movementDuration]; 
self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
[UIView commitAnimations]; 
+0

이것은 나를 위해 작동하지 않았다. 내 업데이 트를 참조하십시오 – Adam