shouldInteractWithURL在3d touch上调用两次

时间:2016-10-17 15:20:52

标签: uitextview 3dtouch

以下是我要修复的问题。我有一个textView,其文本是一个带链接属性的属性字符串。点击链接后,我应该转到其他屏幕。所以,我在textView的shouldInteractWithURL()委托方法上执行该屏幕导航。一切正常,除非强行触摸textView,下一页加载两次。这意味着在强制点击时调用了两次shouldInteractWithURL()。我通过检查视图控制器的堆栈来修复它,如果最后一个VC是即将加载的VC,我将从那里返回。但是,我想知道除此之外是否还有其他解决方案。以下是代码段

    textView.text = ""

    guard var str = myStr else {
        return nil
    }

    let linkAttribute = [NSLinkAttributeName: NSURL(string: "")!]
    var attributedStr:NSMutableAttributedString?
    if delay {
        str += " "
        attributedStr = NSMutableAttributedString(string: str)

        let ctaStr = kSuccessStr
        let ctaAttributedString = NSAttributedString(string: ctaStr, attributes: linkAttribute)
        attributedStr!.appendAttributedString(ctaAttributedString)
    } else {
        let ctaStr = kFailedStr
        attributedStr = NSMutableAttributedString(string: ctaStr, attributes: linkAttribute)
    }
    textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.grayColor()]
    textView.attributedText = attributedStr
    textView.delegate = thisTableViewDelegate

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
    if let textLink = textView.text
    {
        if (textLink.rangeOfString(str1) != nil) ||
        (textLink.rangeOfString(str2) != nil)
        {
            showSignUpForm(self)
        }
        else
        {
            showSuccessfulForm()
        }
    }

    return true
}

2 个答案:

答案 0 :(得分:2)

使用textView:shouldInteractWithURL:inRange:interaction:相反。 检查是否有交互!= UITextItemInteractionInvokeDefaultAction并返回NO。

请参阅https://developer.apple.com/reference/uikit/uitextviewdelegate/1618606-textview?language=objc

答案 1 :(得分:0)

我只处理invokeDefaultAction,它对我有用:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    if interaction == .invokeDefaultAction {
        //do some stuff
    }
    return false
}