美文网首页iOS Developer
iOS在当前屏幕获取第一响应

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

作者: 解忧杂货店老板 | 来源:发表于2016-04-11 09:39 被阅读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...

相关文章

网友评论

    本文标题:iOS在当前屏幕获取第一响应

    本文链接:https://www.haomeiwen.com/subject/zkcilttx.html