【问题标题】:Orientation Keyboard Movement方向键盘移动
【发布时间】:2011-03-25 18:03:59
【问题描述】:

这里有一些关于 portrait 文本字段移动的很棒的教程。

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];
    }
 }
}

移动视图。以下是对应的方法

#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];
}
}

希望您能提供帮助!如果我对问题进行了反消歧(看看我在那里做了什么?),请告诉我!

【问题讨论】:

  • 不确定这两种风景如何不工作..他们将使用相同的代码..有趣。好问题
  • 我认为两者都可以。这有点令人沮丧=)

标签: iphone objective-c keyboard rotation


【解决方案1】:

终于想通了!!!!

问题出在:原点确实在两种横向模式之间发生了变化。因此,您所要做的就是检测您所处的景观版本,并据此进行加减!

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

}

Previous 将执行键盘的实际移动。我去掉了调整大小,你可以重新添加它。

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

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

}

Previous 将确保您正确设置变量 isPortrait 和 leftLandscape。

花了太长时间,但我终于 完成!!!

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 2018-01-12
    • 2019-06-12
    • 2011-07-10
    • 2011-09-22
    • 2018-06-02
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多