方向键盘移动

问题描述:

这里是肖像文本框移动一些伟大的教程。方向键盘移动

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

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