Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UILongPressGestureRecognizer to a UITextField?

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.

like image 595
Azhar Avatar asked May 08 '12 10:05

Azhar


1 Answers

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 :)

like image 177
Malek_Jundi Avatar answered Sep 18 '22 10:09

Malek_Jundi