2011-01-14 77 views

回答

1

我的建議是將您的對象放在UIScrollView上,然後當鍵盤出現時,您可以使用scrollRectToVisible:

[scroller scrollRectToVisible:frame animated:YES]; 
+0

我在這方面很新,你有一個簡單的例子,關於如何做到這一點? 謝謝! – Guerrix

+0

謝謝,我真的很感激。 – Guerrix

7

我已經爲我的一個應用程序寫了這段代碼。

它會自動檢測TextField的位置並相應地滾動baseView。

 



- (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 
{ 
    CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil]; 
    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait){ 

     if(up) { 
      int moveUpValue = temp.y+textField.frame.size.height; 
      animatedDis = 264-(1024-moveUpValue-5); 
     } 
    } 
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     if(up) { 
      int moveUpValue = 1004-temp.y+textField.frame.size.height; 
      animatedDis = 264-(1004-moveUpValue-5); 
     } 
    } 
    else if(orientation == UIInterfaceOrientationLandscapeLeft) { 
     if(up) { 
      int moveUpValue = temp.x+textField.frame.size.height; 
      animatedDis = 352-(768-moveUpValue-5); 
     } 
    } 
    else 
    { 
     if(up) { 
      int moveUpValue = 768-temp.x+textField.frame.size.height; 
      animatedDis = 352-(768-moveUpValue-5); 
     } 

    } 
    if(animatedDis>0) 
    { 
     const int movementDistance = animatedDis; 
     const float movementDuration = 0.3f; 
     int movement = (up ? -movementDistance : movementDistance); 
     [UIView beginAnimations: nil context: nil]; 
     [UIView setAnimationBeginsFromCurrentState: YES]; 
     [UIView setAnimationDuration: movementDuration]; 
     if (orientation == UIInterfaceOrientationPortrait){ 
      baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);  
     } 
     else if(orientation == UIInterfaceOrientationPortraitUpsideDown) { 

      baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement); 
     } 
     else if(orientation == UIInterfaceOrientationLandscapeLeft) { 

      baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement); 
     } 
     else { 
      baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement); 
     } 

     [UIView commitAnimations]; 
    } 
} 

 
+1

謝謝!爲您的代碼:) – Guerrix

+1

它的作品gr8 ..感謝。 – Developer

+0

感謝評論傢伙。 – ValayPatel

1

時候做起來=無變化ValayPatel代碼「如果」語句if(animatedDis>0 || !up)的條件,否則轉移的觀點是總是在前面的位置。

+0

我認爲原始代碼的目的是animatedDis是一個屬性,因此編輯完成後> 0值將被保留,導致視圖動畫到原始位置。 – Noam

相關問題