【问题标题】:animating the virtual keyboard动画虚拟键盘
【发布时间】:2012-07-27 15:41:09
【问题描述】:

我让我的应用检测 iPad 上的方向并相应地重新调整它的位置位置,使其在纵向和横向上都能正常工作。

我在虚拟键盘方法中遇到的问题是我没有考虑方向:在纵向中,一旦用户点击其中一个文本框,该方法就会提升视图以为键盘腾出空间并使文本框可见(不覆盖) - 但是,当用户处于横向并点击文本框时,动画会将视图侧向而不是向上推动,就好像它仍然是纵向的一样。 (同样,如果我将 iPad 倒置,它会将我的视图框架向下而不是向上推)

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)  {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)   {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)   {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;
    // viewFrame.origin.x -= animatedDistance;


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
}

我也有:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
} 

当设备改变方向时,我应该如何更改方法以正确设置动画?

谢谢

【问题讨论】:

    标签: iphone objective-c xcode ipad


    【解决方案1】:

    检测方向并根据该信息决定将视图移动到哪个方向。

    if ([UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeLeft)
        viewFrame.origin.x -= animatedDistance;
    if ([UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeRight)
        viewFrame.origin.x += animatedDistance;
    if ([UIDevice currentDevice].orientation==UIDeviceOrientationPortrait)
        viewFrame.origin.y -= animatedDistance;
    if ([UIDevice currentDevice].orientation==UIDeviceOrientationPortraitUpsideDown)
        viewFrame.origin.y += animatedDistance;
    

    【讨论】:

    • 在代码中我有 viewFrame.origin.y -= animatedDistance;所以它应该提升视图,因为当我处于纵向时它确实可以,但在横向中,如果你明白我的意思,向上是横向的(origin.y 的行为就像它是横向的起源),我不确定我是什么失踪
    • 就像一个魅力。 (为了提供信息,我不得不在文本框退出后撤消这一举动。)谢谢伙计。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2015-01-02
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多