2011-01-14 6 views

答えて

1

あなたのオブジェクトをUIScrollViewに置くことをお勧めします。キーボードが表示されたら、scrollRectToVisible:を使用できます。

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

これは新しくて、これはどうやって簡単な例がありますか? ありがとう! – Guerrix

+0

本当にありがとう、ありがとう。 – Guerrix

7

私は自分のアプリケーションの1つのためにこのコードを書いています。

TextFieldの位置を自動的に検出し、それに応じてベースビューをスクロールします。

 



- (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..thanksを動作させます。 – Developer

+0

コメントありがとうございました。 – ValayPatel

1

は他のシフトビューは、前の位置に常にある、ときNO =アップ状態のための文の「if」if(animatedDis>0 || !up)にValayPatelコードを変更できます。

+0

元のコードはanimatedDisがプロパティであることを意図していたため、編集が終了すると> 0の値が保持され、ビューが元の位置にアニメートされます。 – Noam

関連する問題