Detect text selection about to happen
Is there a way, in iOS, to write code that is called when the sys开发者_开发知识库tem is about to display the text selection controls on a UIWebView?
I need some of my code to be called when the user wants to select something, but before the selection is actually added to the page, if possible.
I found a way around this using delayed method calls. The selection controls do not appear immediately, but only after you tap and hold over some text for about half a second.
Here's a rough outline of the code I used to do that:
//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
}
Not the best solution, but it works in my situation. The delay can be tweaked if textSelectionWillAppear: needs more time to run, but I would think the delay shouldn't get too close to 0.5f or the method may not actually get called before the selection box appears.
精彩评论