【问题标题】:How do I determine when the background area is tapped?如何确定何时点击背景区域?
【发布时间】:2011-02-28 23:28:55
【问题描述】:

我试图确定用户何时点击屏幕上的单个 UITextView 以外的某个区域。这类似于question about UITableViews,但我对那里提出的解决方案有一些问题。当键盘关闭时,我稍微滚动屏幕以隐藏键盘所在的位置。我的问题是,当我使用 UITapGestureRecognizer 确定屏幕是否被点击时,点击不会进入屏幕上的其他控件。我正在使用gestureRecognizer.cancelsTouchesInView = NO,这是时间问题。在控件识别出它已被单击之前,屏幕滚动离开。知道如何解决这个问题吗?我非常乐意使用手势识别以外的东西。

【问题讨论】:

    标签: iphone cocoa-touch ios uiscrollview


    【解决方案1】:

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

    【讨论】:

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

    保留手势识别器。让它使用这样的方法:

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

    【讨论】:

      【解决方案3】:

      我找到了问题的解决方案。我仍在使用 UITapGestureRecognizer,但我现在在隐藏键盘之前有一个零长度延迟,这允许点击事件正确传播到子视图,例如按钮。基本视图是填充屏幕的文本字段和按钮控件。为了在显示键盘时能够正确查看屏幕,整个内容被包裹在滚动视图中,并且键盘占用区域的占位符视图被添加到底部。只有在显示键盘时才会展开。以下是所有相关的代码片段,它们应该允许任何人实现点击任意位置以关闭键盘的想法以及解决控件被键盘隐藏的问题:

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

      希望有人觉得这很有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        • 2013-09-20
        • 1970-01-01
        • 2019-05-09
        • 1970-01-01
        相关资源
        最近更新 更多