简体   繁体   English

通过LongPress更改UITextField BackGround颜色

[英]change UITextField BackGround Color by LongPress

All Boxes is UITextField s. 所有Box是UITextField I want change background color when user long press on UITextField . 当用户长按UITextField时,我想更改背景颜色。
Which TextField had a long press that UITextField color is change, not all UITextField s. 长按哪个TextField可以更改UITextField的颜色,而不是所有UITextField的颜色。

在我看来

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

You can use UILongPressGestureRecognizer . 您可以使用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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM