Obj-C - 通过点击UITextView屏幕上的任何位置来关闭键盘?

时间:2017-10-22 04:55:34

标签: ios objective-c uitextview uitapgesturerecognizer

当我的用户点击我的UITextView(不是uitextfield ...)时,就会出现动画。然而,在UITextView开始编辑之后,似乎没有其他UITapGesture被识别(例如,如果我向我的UIView添加tapGesture以关闭此动画,它根本不执行)。我一直试图解决这个问题,感觉就像永远。感谢帮助,我很难过。

ViewController.m

 - (void)viewDidLoad 
        {
            [super viewDidLoad];

         self.replyField.delegate = self;

        [self.replyField setUserInteractionEnabled:YES];


        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped)];
        [self.view addGestureRecognizer:gestureRecognizer];

        }

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

        return YES;

    }


    -(void)textViewTapped {
        NSLog(@"DISMISS PLEASE!");
        [self animateTextView:NO];

    }

       - (void)textViewDidBeginEditing:(UITextView *)textView
    {

        [self animateTextView: YES];
        self.replyField.gestureRecognizers = nil;


    }


    - (void)textViewDidEndEditing:(UITextView *)textView
    {
        [self animateTextView:NO];


    }


    - (void) animateTextView:(BOOL) up
    {
        const int movementDistance = 206;
        const float movementDuration = 0.3f;
        int movement= movement = (up ? -movementDistance : movementDistance);
        NSLog(@"%d",movement);

        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
        [UIView commitAnimations];



    }


    - (void)textViewDidChange:(UITextView *)textView
    {

    }

5 个答案:

答案 0 :(得分:1)

我遇到类似的问题使用以下代码通过点击屏幕视图上的任意位置来关闭键盘。希望对你有所帮助。

目标c

- (void)viewDidLoad 
{
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;

}
- (void)dismissKeyboard
{
     [self.view endEditing:YES];
}

Swift 4

func viewDidLoad() {
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
    view.addGestureRecognizer(gestureRecognizer)
    gestureRecognizer.cancelsTouchesInView = false
 }

func dismissKeyboard() {
    view.endEditing(true)
}

答案 1 :(得分:1)

您的问题与手势识别器无关。 由行引起的问题:

self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);

您可以使用偏移量从 self.inputView 设置 self.view 的框架。

self.view 大小更改时,其子视图(包括文本视图)可能具有超出视图范围的位置。如果触摸点超出超视范围边界,UIView不响应触摸事件。 您可以通过设置视图的背景颜色并在日志中显示它们来轻松检查:

...
- (void) animateTextView:(BOOL) up
{
    NSLog(@"self.view state1:%@", self.view);
    NSLog(@"self.replyField state1:%@", self.replyField);
    self.view.backgroundColor = [UIColor redColor];
    self.replyField.backgroundColor = [UIColor blueColor];

    const int movementDistance = 206;
    const float movementDuration = 0.3f;
    int movement= movement = (up ? -movementDistance : movementDistance);
    NSLog(@"%d",movement);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    //self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
    self.replyField.frame = CGRectOffset(self.replyField.frame, 0, movement);
    [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
    [UIView commitAnimations];
}

- (void)afterAnimationStops {
    NSLog(@"self.view state2:%@", self.view);
    NSLog(@"self.replyField state2:%@", self.replyField);
}
...

另外什么是self.inputView?它设置得当吗?

BTW:代码的目标是什么?是否要移动文本视图以避免键盘重叠?然后考虑将其放在UIScrollView中。 例如,请参见此处的讨论:How to make a UITextField move up when keyboard is present?

答案 2 :(得分:0)

在ViewDidLoad中:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];

创建一个方法:

-(void)dismissKeyboard {

    [textView resignFirstResponder];
}

答案 3 :(得分:0)

不清楚你想要什么BTW让我解释一下。

首先,如果您想要隐藏键盘,可以通过单行代码完成。

目标C

[self.view endEditing:YES];

<强>夫特

 view.endEditing(true)

现在在您的代码中,您在self.view上添加了点击手势,这意味着如果您在任何地方触摸主视图,那么您的手势方法将被调用。如果您想要在我触摸主视图时隐藏我的键盘,那么在点按手势的方法中,您应该在上面写代码[self.view endEditing:YES];但请确保键盘将打开如果你点击你的textView。

但是如果你想在我点击UITextView时不打开键盘,那么有很多方法可以按照以下方式进行操作

目标C

UIView* dummyInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
myTextView.inputView = dummyInputView;

<强>夫特

let dummyInputView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
myTextView.inputView = dummyInputView

此处键盘不会打开,但如果您想隐藏光标,光标会闪烁,然后您可以通过以下代码执行

目标C

 myTextView.tintColor = [UIColor clearColor];

<强>夫特

 myTextView.tintColor = UIColor.clear

如果您有任何问题,请进行评论。

答案 4 :(得分:-2)

在viewController中你可以这样做

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:Yes];
}