Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何确定何时点击背景区域?_Iphone_Cocoa Touch_Ios_Uiscrollview - Fatal编程技术网

Iphone 如何确定何时点击背景区域?

Iphone 如何确定何时点击背景区域?,iphone,cocoa-touch,ios,uiscrollview,Iphone,Cocoa Touch,Ios,Uiscrollview,我试图确定用户何时点击屏幕上的单个UITextView之外的某个区域。这与此类似,但我对这里介绍的解决方案有一些问题。当键盘关闭时,我稍微滚动屏幕以隐藏键盘所在的位置。我的问题是,当我使用UITapGestureRecognizer确定屏幕是否被点击时,点击不会传递到屏幕上的其他控件。我正在使用手势识别器。cancelsTouchesInView=否,这是时间问题。在控件识别出它被单击之前,屏幕将滚动消失。你知道我怎样才能解决这个问题吗?我非常乐意使用手势识别以外的东西。保留手势识别器。让它使用

我试图确定用户何时点击屏幕上的单个UITextView之外的某个区域。这与此类似,但我对这里介绍的解决方案有一些问题。当键盘关闭时,我稍微滚动屏幕以隐藏键盘所在的位置。我的问题是,当我使用UITapGestureRecognizer确定屏幕是否被点击时,点击不会传递到屏幕上的其他控件。我正在使用手势识别器。cancelsTouchesInView=否,这是时间问题。在控件识别出它被单击之前,屏幕将滚动消失。你知道我怎样才能解决这个问题吗?我非常乐意使用手势识别以外的东西。

保留手势识别器。让它使用如下方法:

- (void)dismissKeyboard:(UIGestureRecognizer *)gesture
{
    [self.view endEditing:NO];
}

在过去,我使用了一个自定义(不可见)按钮作为背景层来执行此操作。

我找到了解决问题的方法。我仍在使用UITapGestureRecognitor,但现在在隐藏键盘之前有一个零长度延迟,这允许点击事件正确传播到子视图,例如按钮。基本视图是一个文本字段和填充屏幕的按钮控件。为了在显示键盘时正确查看屏幕,整个屏幕被包装在一个滚动视图中,并且在底部添加了一个键盘占据区域的占位符视图。它仅在显示键盘时展开。以下是所有相关的代码片段,任何人都可以通过这些代码实现点击任意位置来消除键盘想法,并解决键盘隐藏控件的问题:

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideKeyboardWithDelay)] autorelease];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer: tapRecognizer];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillBeShown:) name: UIKeyboardWillShowNotification object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillBeHidden:) name: UIKeyboardWillHideNotification object: nil];
}

- (void) hideKeyboardWithDelay {
    // using afterDelay allows the event to go through to any button before scrolling
    [self performSelector: @selector(hideKeyboard) withObject: nil afterDelay: 0.0f];
}

- (void) hideKeyboard {
    [self.myTextField1 resignFirstResponder];
    [self.myTextField2 resignFirstResponder];
}


- (void) keyboardWillBeShown: (NSNotification *) notification {
    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect frame = self.keyboardPlaceholder.frame;
    self.keyboardPlaceholder.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, keyboardSize.height);
    CGFloat contentHeight = self.scrollView.contentSize.height + keyboardSize.height + 20; // in my case, save 20px for the status bar
    self.scrollView.contentSize = CGSizeMake(frame.size.width, contentHeight);
    [self.scrollView scrollRectToVisible: self.keyboardPlaceholder.frame animated: YES];
}

- (void) keyboardWillBeHidden: (NSNotification *) notification {
    CGRect frame = self.keyboardPlaceholder.frame;
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0.3f; // default keyboard animation time
    [value getValue: &duration];

    [UIView beginAnimations: @"hideKeyboardAnimation" context: nil];
    [UIView setAnimationDuration: duration];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];

    self.keyboardPlaceholder.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 0);   
    self.scrollView.contentSize = self.view.frame.size;

    [UIView commitAnimations]; 
}

- (void)viewDidUnload { 
    [[NSNotificationCenter defaultCenter] removeObserver: self];
    [super viewDidUnload];
}

希望有人觉得这个有用

我也这样做了,但是我已经布置了子视图,这样我就必须添加几个按钮才能工作(以及在我的常规按钮回调中包括键盘处理),我需要在多个屏幕中使用这个概念,所以我想找到一个更通用的解决方案。这些子视图需要接受点击吗?不可见的按钮层是否可以在层之间?我听到了,不过,我相信有一个优雅的通用解决方案。如果我能想出什么,我会继续寻找并公布结果