2011-01-14 125 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

相关问题