美文网首页
iOS - UITextView实现原理

iOS - UITextView实现原理

作者: SealShile | 来源:发表于2018-01-20 00:11 被阅读201次
    open class UITextView : UIScrollView, UITextInput, UIContentSizeCategoryAdjusting {}
    

    UITextView继承于UIScrollView, UITextInput, UIContentSizeCategoryAdjusting,当然UIScrollView继承于UIView,UIView继承于UIResponder。

    所以我们从UITextView的View hierarchy和Responder chain来分析。

    View hierarchy

    1. UITextView
    2. UITextContainerView
    3. UITextSelectionView(出现光标的时候会有这个View)

    Responder

    UITextView对UITouch的处理主要是靠如下几个GestureRecognizer。

    <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x12ed4a540;
    state = Possible; 
    delaysTouchesBegan = YES; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=delayed:,target=<SealFooDemo.SLTextView 0x12f81fa00>)>>
    
    <UIScrollViewPanGestureRecognizer: 0x12ed4aff0;
    state = Possible;
    delaysTouchesEnded = NO; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=handlePan:, target=<SealFooDemo.SLTextView 0x12f81fa00>)>>
    
    <UITextTapRecognizer: 0x12ed55e70; 
    state = Possible; 
    delaysTouchesEnded = NO; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=oneFingerDoubleTap:, target=<UITextInteractionAssistant 0x12ed55800>)>;
    numberOfTapsRequired = 2>
    
    <UITapAndAHalfRecognizer: 0x12ed56d40; 
    state = Possible; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=tapAndAHalf:, target=<UITextInteractionAssistant 0x12ed55800>)>>
    
    <UITextTapRecognizer: 0x12ed588a0; 
    state = Possible; 
    delaysTouchesEnded = NO; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=oneFingerTap:, target=<UITextInteractionAssistant 0x12ed55800>)>>
    
    <UIVariableDelayLoupeGesture: 0x12ed58ed0; 
    state = Possible; 
    delaysTouchesEnded = NO; 
    view = <SealFooDemo.SLTextView 0x12f81fa00>;
    target= <(action=loupeGesture:, target=<UITextInteractionAssistant 0x12ed55800>)>>
    

    这里的UITextTapRecognizer是UITapGestureRecognizer的子类,但是在UIKit里面却找不到,还不知道原因。

    for gestureRecognizer:UIGestureRecognizer in self.textView.gestureRecognizers! {
        print(gestureRecognizer.cancelsTouchesInView)
    }
    

    执行上面代码的结果都是True,因为所有UIGestureRecognizer的cancelsTouchesInView默认都是false,这样只要是有UIGestureRecognizer的地方,view都是拿不到被识别走的UITouch的。

    相关文章

      网友评论

          本文标题:iOS - UITextView实现原理

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