【问题标题】:Multiline UITextView in UIScrollView selection not workingUIScrollView 选择中的多行 UITextView 不起作用
【发布时间】:2014-12-19 18:54:20
【问题描述】:

我有一个位于 UIScrollView 中的多行 UITextView。它被配置为可编辑和可选择的,因此长按会调出标准的全选/全选等菜单。如果我点击全选,我只能调整第一行的选择大小。一旦选择更改为不包括第一行,选择句柄就不再响应触摸输入。

如果我在第一行选择一个单词,左选择手柄正常工作,但右手柄不接收触摸输入。

任何想法可能导致这种情况?这很奇怪,我真的不知道发生了什么。我没有压倒任何手势(无论如何我都知道)。

【问题讨论】:

    标签: ios uiscrollview uitextview selection


    【解决方案1】:

    啊,想通了。正如预期的那样,文本视图位于另一个视图中,但我在父视图上进行了自定义命中检测。在 -hitTest:withEvent: 中,在我处于编辑模式的情况下,我返回文本视图本身,而不是在文本视图上调用 hitTest,以便它可以确定是否应该返回选择视图。

    所以,长话短说,如果您在另一个视图中有一个文本视图并且正在执行自定义命中检测,请确保根据需要在 UITextView 上调用 -hitTest:withEvent:,这样您就不会失去选择功能。

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // a text box is tappable if:
    // a) obviously, the tap point is inside self
    // b) if current mode is unselected, check to see if a label was tapped. Because the label stretches the width of
    //    the text box for layout purposes, check to see if the tap point lays inside with intrinsic content size of the
    //    label. If not, pass the tap to the next subview
    // c) If the mode is already selected, ask super to return the appropriate view.
    
    if (!self.userInteractionEnabled || self.hidden || self.alpha <= 0.01) {
        return nil;
    }
    
    CGRect touchRect = self.bounds;
    
    if (self.currentMode == TextBoxModeSelected) {
        touchRect = CGRectInset(self.bounds, -22, -22);
    }
    
    if (CGRectContainsPoint(touchRect, point)) {
        if (self.currentMode == TextBoxModeUnselected) {
            return [self.readOnlyTextView hitTest:point withEvent:event];
        }
        else if (self.currentMode == TextBoxModeSelected) {
            return self;
        }
        else if (self.currentMode == TextBoxModeEdit) {
            CGPoint pointInTextView = [self.editingTextView convertPoint:point fromView:self];
    
            if ([self.editingTextView pointInside:pointInTextView withEvent:event]) {
                // PREVIOUSLY I WAS RETURNING self.editingTextView HERE
                // self.contentView IS THE TEXT VIEW'S PARENT. LET IT DETERMINE THE 
                // SUBVIEW THAT'S BEING TAPPED. IN THE CASE OF SELECTION, IT'S UITextRangeView
                return [self.contentView hitTest:point withEvent:event];
            }
        }
    }
    
    return nil;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多