How to add UILongPressGestureRecognizer to a UITextField?

旧巷老猫 提交于 2019-12-22 05:10:12

问题


I am trying yo add UILongPressGestureRecognizer to one of UITextField on page but It does not call the selector method when Long Press the UiTextField. I added it to UItextField But it does not call the Selector method when I Long press the TextField but Showing the Magnifier on Field.

[self.tfCustomerStreet addGestureRecognizer:LongPressgesture];

But it works fine and call the selector Method if I add it to the View.

[[self view] addGestureRecognizer:LongPressgesture];

Initialization code in ViewDidLoad

UILongPressGestureRecognizer *LongPressgesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressgesture:)];
    [LongPressgesture setMinimumPressDuration:2.0];

.

// Long press gesture reconizer
- (void)LongPressgesture:(UILongPressGestureRecognizer *)recognizer 
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended .................");
    }
    else {
        NSLog(@"Long press detected .....................");
    }        
}

Please tell me How do I make it work with UITextField.


回答1:


if you remove the [LongPressgesture setMinimumPressDuration:2.0]; it will work .. since the tab gesture will be called to start edit the textField ... or just implement this gesture delegate function

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

returning YES to this method is guaranteed to allow simultaneous recognition.

Enjoy :)



来源:https://stackoverflow.com/questions/10497152/how-to-add-uilongpressgesturerecognizer-to-a-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!