我正在尝试将UILongPressGestureRecognizer添加到UITextfield;但是,我似乎无法让它表现得很好。我想要的是覆盖默认的长按行为(放大镜和“粘贴”选项),但也保持在文本字段中点击以键入它的能力。使用下面的代码,它仍会显示放大镜,即使它进入textFieldLongPressed
方法,它也永远不会有正确的状态。有什么想法吗?
- (void) addLongPressRecognizer: (UIView *) view
{
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[gestureRecognizer addTarget:self action:@selector(textFieldLongPressed:)];
gestureRecognizer.delegate = self;
[view addGestureRecognizer: gestureRecognizer];
self.myGestureReconginzer = gestureRecognizer;
}
- (void) textFieldLongPressed:(UILongPressGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
[self.delegate textFieldLongPressed:self];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if(gestureRecognizer == self.myGestureReconginzer)
{
//I've tried it with and without the following line
// [self textFieldLongPressed:(UILongPressGestureRecognizer*) gestureRecognizer];
return YES;
}
else if([gestureRecognizer class] == [UILongPressGestureRecognizer class])
{
return NO;
}
return YES;
}
答案 0 :(得分:0)
尝试将这样的内容添加到您的UITextField子类中:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
return [super canPerformAction:action withSender:sender];
}
这应禁用粘贴选项。