2011-01-14 12 views

Antwort

1

Mein Vorschlag ist, Ihre Objekte auf einem UIScrollView zu platzieren, dann, wenn die Tastatur angezeigt wird, können Sie scrollRectToVisible: verwenden.

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

Ich bin nett neu dazu, hast du ein einfaches Beispiel darüber, wie das geht? Danke! – Guerrix

+0

Danke ich schätze es wirklich. – Guerrix

7

Ich habe diesen Code für eine meiner Anwendung geschrieben.

Es erkennt automatisch die Position des TextField und Scroll die BaseView Dementsprechend.

 



- (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

Danke !! für Ihren Code :) – Guerrix

+1

funktioniert es gr8..thanks. – Developer

+0

Danke für die Kommentare Jungs. – ValayPatel

1

Do ValayPatel Code "if" Anweisung if(animatedDis>0 || !up) für Zustand, wenn nach oben = NO, sonst die verschobene Ansicht ist immer bei vorheriger Position ändern.

+0

Ich glaube, dass der ursprüngliche Code animatedDis eine Eigenschaft sein soll, und daher wird der Wert> 0 beibehalten, wenn die Bearbeitung beendet wird, wodurch die Ansichten an ihre ursprüngliche Position animiert werden. – Noam

Verwandte Themen