Ios 通过LongPress更改UITextField背景色

Ios 通过LongPress更改UITextField背景色,ios,uitextfield,Ios,Uitextfield,所有框都是UITextFields。我想在用户长按UITextField时更改背景色 哪个文本字段长按了一次,表示UITextField的颜色发生了变化,而不是所有的UITextFields 您可以使用ui长按手势识别器。请注意,一个手势识别器可以附加到一个视图。 下面是一个代码示例 - (void)viewDidLoad { [super viewDidLoad]; //Array that holds your textfields NSArray *myTextFi

所有框都是
UITextField
s。我想在用户长按
UITextField
时更改背景色
哪个文本字段长按了一次,表示UITextField的颜色发生了变化,而不是所有的
UITextField
s


您可以使用
ui长按手势识别器
。请注意,一个手势识别器可以附加到一个视图。
下面是一个代码示例

- (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];
}

您可以使用
ui长按手势识别器
。请注意,一个手势识别器可以附加到一个视图。
下面是一个代码示例

- (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];
}

试着像这样使用

- (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]];
}

试着像这样使用

- (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]];
}

当长按文本字段时,它会显示放大镜视图和键盘,但不会更改背景颜色。它在我的末端扭曲。因为我已经实现了它。看到我编辑过的答案,我已经做了一点修改。当我长按文本字段时,它不会转到-(void)changeBackground:(UIgestureRecognitor*)gs,它会变贵。我认为你做错了什么事,我很肯定。这段代码在我这边正常工作。当长按文本字段时,它会显示放大镜视图和键盘,但不会更改背景颜色。它在我这边扭曲。因为我已经实现了它。看到我编辑过的答案,我已经做了一点修改。当我长按文本字段时,它不会转到-(void)changeBackground:(UIgestureRecognitor*)gs,它会变贵。我认为你做错了什么事,我很肯定。这段代码在我这端正常工作。当长按文本字段时,显示放大镜视图,键盘显示,但不更改背景色。当长按文本字段时,显示放大镜视图,键盘显示,但不更改背景色