2

All Boxes is UITextFields. I want change background color when user long press on UITextField.
Which TextField had a long press that UITextField color is change, not all UITextFields.

In My View

4

2 回答 2

3

Try to use like this...

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILongPressGestureRecognizer *gs = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(changeBackground:)];
    [textFld addGestureRecognizer:gs];
}

- (void)changeBackground:(UIGestureRecognizer *)gs
{
     [self.view endEditing:YES]; // Edited 

     UITextField *txtFld = (UITextField *)gs.view;

     [txtFld setBackgroundColor:[UIColor redColor]];
}
于 2013-09-07T11:26:08.477 回答
1

You can use UILongPressGestureRecognizer. Please note that one gesture recognizer can be attached to one view.
Here is code example

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Array that holds your textfields
    NSArray *myTextFields;
    for (UITextField *textField in myTextFields) {
        //Creating UILongPressGestureRecognizer
        UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        //Attaching it to textfield
        [textField addGestureRecognizer:longPressGestureRecognizer];
    }
}
//Handling long press
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    UITextField *textField = (UITextField *)gestureRecognizer.view;
    textField.backgroundColor = [UIColor greenColor];
}
于 2013-09-07T11:23:18.977 回答