如何在UITextField上模拟双击?

时间:2013-11-22 13:59:47

标签: ios iphone objective-c uigesturerecognizer tap

当我在UITextfield中写一些东西并双击它时,它会在ios设备上显示“选择”,“全选”,“粘贴”选项。我希望通过编程显示相同的信息。

任何人都可以建议这样做吗?

使用以下代码,双击不会引发火灾。

+(void)performanualTouch:(UITouch*)touch {
 UIEvent *eventDown = [[UIEvent alloc] initWithTouch:touch];


    for (UIGestureRecognizer* gr in touch.view.gestureRecognizers) {       

        if ([gr isKindOfClass:[UITapGestureRecognizer class]]) {
            [gr touchesBegan:[eventDown allTouches] withEvent:eventDown];
            [touch setThePhase:UITouchPhaseEnded];
            UIEvent *eventUp = [[UIEvent alloc] initWithTouch:touch]; 
            [gr touchesEnded:[eventUp allTouches] withEvent:eventUp];
            [eventUp release];


            if (gr.cancelsTouchesInView) {

                return;
            }

        }
    }


 [touch.view touchesBegan:[eventDown allTouches] withEvent:eventDown];
 [[UIApplication sharedApplication] sendEvent:eventDown];
 [touch setThePhase:UITouchPhaseEnded];
 UIEvent *eventUp = [[UIEvent alloc] initWithTouch:touch];
 [touch.view touchesEnded:[eventUp allTouches] withEvent:eventUp];
 [[UIApplication sharedApplication] sendEvent:eventUp];

 [eventDown release];
 [eventUp release];

}

谢谢

1 个答案:

答案 0 :(得分:0)

您可以找到涵盖此特定需求的文档here

相关部分:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject];

if ([theTouch tapCount] == 2  && [self becomeFirstResponder]) {

    // selection management code goes here...

    // bring up edit menu.
    UIMenuController *theMenu = [UIMenuController sharedMenuController];
    CGRect selectionRect = CGRectMake (currentSelection.x, currentSelection.y, SIDE, SIDE);
    [theMenu setTargetRect:selectionRect inView:self];
    [theMenu setMenuVisible:YES animated:YES];

}
}