在 iOS 中,有没有办法编写在系统即将在 UIWebView 上显示文本选择控件时调用的代码?
当用户想要选择某些东西时,我需要调用我的一些代码,但如果可能的话,在实际将选择添加到页面之前。
在 iOS 中,有没有办法编写在系统即将在 UIWebView 上显示文本选择控件时调用的代码?
当用户想要选择某些东西时,我需要调用我的一些代码,但如果可能的话,在实际将选择添加到页面之前。
我找到了一种使用延迟方法调用的方法。选择控件不会立即出现,但只有在您点击并按住某些文本约半秒钟后才会出现。
这是我用来执行此操作的代码的粗略概述:
//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,否则在选择框出现之前可能实际上不会调用该方法。