将UIKeyboardFrameEndUserInfoKey转换为视图或窗口坐标

时间:2013-03-14 06:01:55

标签: iphone ios cocoa-touch uiview uikeyboard

对于常量UIKeyboardFrameEndUserInfoKey,在Apple文档中说:

  

这些坐标不考虑任何旋转因子   作为界面方向的结果应用于窗口的内容   变化。因此,您可能需要将矩形转换为窗口   坐标(使用convertRect:fromWindow:方法)或查看   在使用之前坐标(使用convertRect:fromView:方法)。

所以,如果我使用[view1 convertRect:rect fromView:view2]

我会为上述参数插入什么来让它正确转换旋转值?即:

view1 =? rect =? (我假设的键盘架) view2 =?

尝试了一些事情并获得了一些有趣的东西。

3 个答案:

答案 0 :(得分:68)

第一个视图应该是您的视图。第二个视图应为零,表示窗口/屏幕坐标。因此:

NSDictionary* d = [notification userInfo];
CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [myView convertRect:r fromView:nil];

现在,根据您的视图,键盘将占用矩形。如果您的视图是当前视图控制器的视图(或其子视图),则现在考虑旋转等。

答案 1 :(得分:23)

我尝试了接受的答案,发现它实际上并没有在视图中提供键盘的CGRect。我发现我必须将CGRect从UIScreen对象转换为UIWindow对象,并从UIWindow对象转换为UIView对象:

NSValue * keyboardEndFrame;
CGRect    screenRect;
CGRect    windowRect;
CGRect    viewRect;

// determine's keyboard height
screenRect    = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
windowRect    = [self.view.window convertRect:screenRect fromWindow:nil];
viewRect      = [self.view        convertRect:windowRect fromView:nil];

我使用上面的内容来调整根视图的大小,使其不被键盘隐藏:

NSTimeInterval  duration;
CGRect          frame;

// determine length of animation
duration  = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

// resize the view
frame              = self.view.frame;
frame.size.height -= viewRect.size.height;

// animate view resize with the keyboard movement
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:duration];
self.view.frame = frame;
[UIView commitAnimations];

答案 2 :(得分:1)

+ (void)parseKeyboardNotification:(NSNotification *)notification
                 inRelationToView:(UIView *)view
                             info:(void(^)(NSTimeInterval keyboardAnimationDuration, CGRect keyboardFrameInView, UIViewAnimationOptions keyboardAnimationOptions))callback
{
    NSParameterAssert(notification != nil);
    NSParameterAssert(view != nil);

    NSDictionary *userInfo = [notification userInfo];

    UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    UIViewAnimationOptions animationOption = animationCurve << 16; // https://devforums.apple.com/message/878410#878410
    NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // http://stackoverflow.com/a/16615391/202451
    CGRect screenRect    = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect windowRect    = [view.window convertRect:screenRect fromWindow:nil];
    CGRect viewRect      = [view        convertRect:windowRect fromView:nil];

    callback(animationDuration, viewRect, animationOption);
}

可以像这样使用

- (void)keyboardWillShowOrHide:(NSNotification *)notification
{    
    [AGKeyboardInfo parseKeyboardNotification:notification inRelationToView:self.view info:^(NSTimeInterval keyboardAnimationDuration, CGRect keyboardFrameInView, UIViewAnimationOptions keyboardAnimationOptions) {

        [UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationOptions animations:^{

             // do any modifications to your views

        } completion:nil];
    }];
}