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

时间:2011-11-23 05:20:21

标签: iphone ipad ios4 xcode4

我正在学习过程中,我有这个特殊的代码,因为每当我们点击文本字段或文本视图时屏幕向上移动,当我们重新键入键盘时,屏幕就会到达其原始位置,它的工作正常对于风景和人像模式

问题是什么。最初它处于纵向模式,当我们点击文本字段或文本视图时屏幕正在升起并显示键盘,现在当我旋转到横向并重新调整键盘时,屏幕向下并且反之亦然

    - (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 个答案:

答案 0 :(得分:1)

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