iOS Developer

iOS在当前屏幕获取第一响应

2016-04-11  本文已影响2019人  解忧杂货店老板

当你想在点击某个输入框时弹出键盘,并且把UITableView或者ScrollView滚动到某个对应界面时,首先得获取第一相应是谁;

代码:

    //首先通过这两行代码获取第一相应
    UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView * firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    //然后再通过判断响应类做出相应的响应
    CGPoint firstPoint = CGPointZero;
    if ([firstResponder isKindOfClass:[UITextField class]]) {
        firstPoint = CGPointFromString(_firstResponderPointArray[0]);
    } else if ([firstResponder isKindOfClass:[UITextView class]]) {
        firstPoint = CGPointFromString(_firstResponderPointArray[1]);
    }
    NSDictionary * info = [aNotification userInfo];
    NSValue * value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    if (keyboardSize.height > 0) {
        [UIView animateWithDuration:[FMSize getInstance].defaultAnimationDuration animations:^{
            _scrollView.frame = CGRectMake(0, 0, _realWidth, _realHeight-keyboardSize.height);
            [_scrollView setContentOffset:firstPoint];
            _scrollView.bouncesZoom = NO;
        }];
    }

This all...

上一篇下一篇

猜你喜欢

热点阅读