如何将y轴设置为0,如果屏幕超出了原始屏幕或屏幕下方 - 在iPad

问题描述:

我在学习过程中,我有这个特定的代码,只要我们点击文本框或textview屏幕向上移动,当我们退出键盘屏幕到达其原始位置时,它对于横向和纵向模式均可正常工作如何将y轴设置为0,如果屏幕超出了原始屏幕或屏幕下方 - 在iPad

问题是什么。最初它是在肖像模式下,当我们点击文本框或文本视图时,屏幕正在抬起并显示键盘,现在当我旋转到横向并退出键盘时,屏幕正在向下移动,反之亦然。

- (void)animateTextField:(UITextField*)textField textView:(UITextView *)textView up:(BOOL)up{ 
      int animatedDistance; 
      int moveUpValue; 

      if (textField) { 
       moveUpValue = textField.frame.origin.y+ textField.frame.size.height; 
       } 
      else { 
       moveUpValue = textView.frame.origin.y+ textView.frame.size.height; 
      } 

      NSLog(@"moveup%d",moveUpValue); 

      UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

      if (orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) 
       { 
       if(textField){ 
        animatedDistance = 216-(650-moveUpValue-5); 
       } 
       else{ 
       animatedDistance = 216-(850-moveUpValue-5); 
       } 
       } 
      else 
      { 
       if(textField){ 
        animatedDistance = 162-(505-moveUpValue-5); 
       } 
       else{ 
        animatedDistance = 162-(750-moveUpValue-5); 
      } 
     } 
     NSLog(@"animated%d",animatedDistance); 

     if(animatedDistance>0) 
     { 
      const int movementDistance = animatedDistance; 
      const float movementDuration = 0.3f; 
      int movement = (up ? -movementDistance : movementDistance); 
      NSLog(@"movement portrait%d",movement); 
      [UIView beginAnimations: nil context: nil]; 
      [UIView setAnimationBeginsFromCurrentState: YES]; 
      [UIView setAnimationDuration: movementDuration]; 
      scrollview.frame = CGRectOffset(scrollview.frame, 0, movement);  
      [UIView commitAnimations]; 

     } 
    } 

我想现在我该如何解决这个问题,或者怎样才能改变y轴为0

请您做的帮助。 在前提前感谢

+1

您可以使用方向委托方法按照新方向更新animatedDistance。所以当你辞职键盘时,animatedDistance将按照currentOrientation。 – Ravin

+1

请参考以下链接你的答案.. http://*.com/questions/8090063/how-to-manage-application-when-orientation-changes/8090119#8090119 –

+0

我已经使用了,和 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { //返回YES用于支撑取向 如果(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ [自portraitFrames]; } else { [self landscapeFrames]; } return YES; } – iYahoo

验证设备方向更改时的animatedDistance设置。