【问题标题】:Detect text selection about to happen检测即将发生的文本选择
【发布时间】:2011-12-07 01:38:51
【问题描述】:

在 iOS 中,有没有办法编写在系统即将在 UIWebView 上显示文本选择控件时调用的代码?

如果可能的话,我需要在用户想要选择某些内容时调用我的一些代码,但在实际将选择添加到页面之前。

【问题讨论】:

    标签: ios textselection


    【解决方案1】:

    我找到了一种使用延迟方法调用的方法。选择控件不会立即出现,但只有在您点击并按住某些文本大约半秒钟后才会出现。

    以下是我用来执行此操作的代码的粗略大纲:

    //in the view controller header declare a boolean
       BOOL _confirmUserIsSelectingText;
    
    //in onTouchBegan
       _confirmUserIsSelectingText = YES;
       [self performSelector:@selector(textSelectionWillAppear:) withObject:nil afterDelay:0.3f];
    
    //in onTouchMoved
       _confirmUserIsSelectingText = NO;
    
    //in onTouchEnded
       _confirmUserIsSelectingText = NO;
    
    //then define textSelectionWillAppear:    
    - (void)textSelectionWillAppear:(id)ignoreMe
    {
      //do whatever it is you need to happen before the selection controls appear
    }
    

    不是最好的解决方案,但它适用于我的情况。如果 textSelectionWillAppear: 需要更多时间来运行,则可以调整延迟,但我认为延迟不应太接近 0.5f,否则在选择框出现之前可能实际上不会调用该方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      相关资源
      最近更新 更多