scrollview上的xcode UITapgestureRecognitor直到第二次点击才调用

scrollview上的xcode UITapgestureRecognitor直到第二次点击才调用,xcode,uiscrollview,uigesturerecognizer,Xcode,Uiscrollview,Uigesturerecognizer,如果用户点击背景,我有以下代码来关闭键盘。如果scrollview处于PointZero位置,它可以正常工作,但是如果用户滚动视图,然后选择textview,则直到第二次后台点击,它才会调用dismissKeyboard方法 由于某种原因,在第一次点击时,移动scrollview偏移以与scrollview框架对齐到屏幕底部。第二次轻触将关闭键盘并运行下面的代码。我知道这和滚动视图有关。任何帮助都将不胜感激 谢谢 - (void)viewDidLoad { UITapGestureRec

如果用户点击背景,我有以下代码来关闭键盘。如果scrollview处于PointZero位置,它可以正常工作,但是如果用户滚动视图,然后选择textview,则直到第二次后台点击,它才会调用dismissKeyboard方法

由于某种原因,在第一次点击时,移动scrollview偏移以与scrollview框架对齐到屏幕底部。第二次轻触将关闭键盘并运行下面的代码。我知道这和滚动视图有关。任何帮助都将不胜感激

谢谢

- (void)viewDidLoad {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tapGesture.cancelsTouchesInView = NO;
    [_scrollView addGestureRecognizer:tapGesture];
}

-(void)dismissKeyboard {
    [self.view endEditing:YES];
}

- (void)keyboardWasShown:(NSNotification *)notification {
    scrollViewRect = _scrollView.contentOffset.y;

    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    keyboardSize.height += 10;
    CGFloat viewBottom = CGRectGetMaxY(self.scrollView.frame);

    if ([_itemNotes isFirstResponder]) {
        CGFloat notesBottom = CGRectGetMaxY(_itemNotes.frame);
        viewBottom -= notesBottom;
        if (viewBottom < keyboardSize.height) {
            keyboardSize.height -= viewBottom;

            CGPoint scrollPoint = CGPointMake(0.0, keyboardSize.height);

            [self.scrollView setContentOffset:scrollPoint animated:YES];
        }
        else {
            [self.scrollView setContentOffset:CGPointZero animated:YES];
        }
    }
    else {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification *)notification {
    CGPoint scrollPoint = CGPointMake(0.0, scrollViewRect);
    [self.scrollView setContentOffset:scrollPoint animated:YES];
}
编辑:
所以我想出了一个解决办法,但似乎必须有更好的方法来处理这个问题。问题是因为我正在设置scrollView的contentOffset,以便contentSize超出屏幕边界。因此,第一次点击是将scrollView内容偏移移回屏幕边界内,第二次点击是执行点击手势。我将在下面发布我的解决方案,希望有人能给出更好的答案。

我认为一定有更好的解决方案,但我能够通过在显示键盘时扩展contentSize,然后在隐藏键盘时缩小contentSize来解决问题

设置浮动scrollViewHeight以保持重置的原始内容大小

//add this right before setting the content offset
scrollViewHeight = _scrollView.contentSize.height;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight + keyboardSize.height);

//add this right before reseting the content offset
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight);

看来真的有更好的方法,我不知道。我将不得不回顾文档,看看是否有其他方法。

我建议设置

_scrollView.layer.borderColor = [UIColor redColor].CGColor;
_scrollView.layer.borderWidth = 1;

这将准确地显示滚动视图边界的位置,可能不是您认为的位置,或者可能被其他内容覆盖。此外,当我打开键盘时,我通常会将scrollview框架底部设置为键盘顶部。否则,您可能无法访问键盘下方的内容。不确定这是否与您的问题完全相关。

在我的情况下,我将textview底部设置为键盘顶部,谢谢您的提示。