iPad的bug景观与景观不同

时间:2014-02-24 21:41:09

标签: ios objective-c ipad animation delegates

这是我的问题:

在我的iPad Split View Controller中,我有详细的文本字段,从navigationBar到视图的末尾。 现在我正在使用委托来为视图设置动画,以便让用户在没有键盘隐藏textFields的情况下进行编写。 这是(void)textFieldDidBeginEditing:

中的代码
CGFloat animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

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

    //CGRect viewFrame = self.view.frame;
    CGRect viewFrame = view.frame;

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

        if (detail) // iPad
            viewFrame.origin.x += animatedDistance;
        else // iPhone
            viewFrame.origin.y -= animatedDistance;
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
        if(detail)
            viewFrame.origin.y -= animatedDistance;
        else
            viewFrame.origin.x += animatedDistance;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [view setFrame:viewFrame];
    [UIView commitAnimations];

并且它在UIInterfaceOrientationLandscapeLeft上完美运行,但在UIInterfaceOrientationLandscapeRight上,最后2个textFields没有动画,我无法弄清楚原因!

在发表任何评论之前我是从每个textFields中拖动委托,我说的比UIInterfaceOrientationLandscapeLeft上的我没有问题。如果我改变方向,只有最后2个不移动。

其实我想知道你们是否有同样的问题,如果你设法解决,或者这只是一个错误,或者我的代码有问题:)

1 个答案:

答案 0 :(得分:0)

您可以检查 textFieldRect 尺寸,在iOS 7中,当设备处于横向模式时,高度和宽度值将交换,因此您需要在公式上更改它们。