2011-03-25 112 views
2

這裏是肖像文本框移動一些偉大的教程。方向鍵盤移動

One Two Three

我認爲,在另一方面,旋轉到縱向和橫向,並在兩個方向鍵盤掩蓋文本字段......眼下,無論是人像,和一個的景觀取向工作。

所以我想知道如何包括兩個風景方向。

下面是我在做什麼:

-(void) keyboardWillShow:(NSNotification *)notif{ 
if ([serverIP isFirstResponder]){ 
    if (isPortrait && self.view.frame.origin.y >= 0){ 
     [self setViewMovedVertical:YES]; 
    } 
    else if (!isPortrait && self.view.frame.origin.x >= 0){ 
     [self setViewMovedHorizontal:YES]; 
    } 
} 
} 

要移動視圖。這裏是corrosponding方法

#define PORTRAIT_KEY_OFF 216 
#define LANDSCAPE_KEY_OFF 140 

-(void) setViewMovedVertical:(BOOL)movedUp{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.4]; 

CGRect rect = self.view.frame; 

if (movedUp){ 
    rect.origin.y -= PORTRAIT_KEY_OFF; 
    rect.size.height += PORTRAIT_KEY_OFF; 
} 
else{ 
    rect.origin.y += PORTRAIT_KEY_OFF; 
    rect.size.height -= PORTRAIT_KEY_OFF; 
} 

self.view.frame = rect; 

[UIView commitAnimations]; 
} 

-(void) setViewMovedHorizontal:(BOOL)moved{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.4]; 

CGRect rect = self.view.frame; 

if (moved){ 
    rect.origin.x -= LANDSCAPE_KEY_OFF; 
    rect.size.width += LANDSCAPE_KEY_OFF; 
} 
else{ 
    rect.origin.x += LANDSCAPE_KEY_OFF; 
    rect.size.width -= LANDSCAPE_KEY_OFF; 
} 

self.view.frame = rect; 

[UIView commitAnimations]; 

} 

和這裏的移動它背下來

-(IBAction) serverIPDone: (UITextField *) sender{ 
if ([serverIP isFirstResponder]){ 
if (self.view.frame.origin.y < 0){ 
    [self setViewMovedVertical:NO]; 
} 
if (self.view.frame.origin.x < 0){ 
    [self setViewMovedHorizontal:NO]; 
} 
[serverIP resignFirstResponder]; 
} 
} 

希望你能幫助的方法!如果我反歧視(請參閱我在那裏做了什麼?)問題,請讓我知道!

+0

不知道如何兩個景觀不工作..他們將使用相同的代碼..有趣。好問題 – 2011-03-25 18:24:20

+0

我覺得兩者都有效。這有點令人沮喪=) – ultifinitus 2011-03-26 01:01:50

回答

2

FINALLY想通了!!!!

這裏的什麼是錯的:起源確實兩個橫向模式之間進行切換。所以你所要做的就是檢測你在哪個版本的風景,並根據它來添加或減去!

-(IBAction) serverIPDone: (UITextField *) sender{ 
if ([serverIP isFirstResponder]){ 
     if (self.view.frame.origin.y < 0){ 
      [self setViewMovedVertical:NO]; 
     } 
    if (self.view.frame.origin.x != 0){ 
     [self setViewMovedHorizontal:NO]; 
    } 
    [serverIP resignFirstResponder]; 
} 
} 

以前可以確保鍵盤沒有設置。

-(void) setViewMovedVertical:(BOOL)movedUp{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.4]; 

CGRect rect = self.view.frame; 

if (movedUp){ 
    rect.origin.y -= PORTRAIT_KEY_OFF; 
    rect.size.height += PORTRAIT_KEY_OFF; 
} 
else{ 
    rect.origin.y += PORTRAIT_KEY_OFF; 
    rect.size.height -= PORTRAIT_KEY_OFF; 
} 

self.view.frame = rect; 

[UIView commitAnimations]; 
} 

-(void) setViewMovedHorizontal:(BOOL)moved{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.4]; 

CGRect rect = self.view.frame; 

if (moved){ 
    if (leftLandscape) 
     rect.origin.x -= LANDSCAPE_KEY_OFF; 
    else 
     rect.origin.x += LANDSCAPE_KEY_OFF; 
} 
else{ 
    if (leftLandscape) 
     rect.origin.x += LANDSCAPE_KEY_OFF; 
    else 
     rect.origin.x -= LANDSCAPE_KEY_OFF; 
    NSLog(@"after: %f",rect.origin.x); 

} 

self.view.frame = rect; 

[UIView commitAnimations]; 

} 

的上一頁會做鍵盤的實際運動。我擺脫了調整大小,你可以將它添加回來。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x  
            duration:(NSTimeInterval)duration{ 

if (UIInterfaceOrientationIsPortrait(x)) { 
    isPortrait = TRUE; 
} 
else { 
    isPortrait = FALSE; 
      leftLandscape = (x == UIInterfaceOrientationLandscapeLeft); 
} 

} 

的上一頁將確保你正確設置變量isPortrait和leftLandscape。

花了太長時間,但我終於DONE