简体   繁体   English

如何确定何时拍摄背景区域?

[英]How do I determine when the background area is tapped?

I am trying to determine when a user taps on some area other than the single UITextView I have on the screen. 我试图确定用户何时点击屏幕上的单个UITextView以外的某个区域。 This is similar to this question about UITableViews , but I have a few problems with the solutions presented there. 这与关于UITableViews的这个问题类似,但我在那里提出的解决方案存在一些问题。 When the keyboard is dismissed, I scroll the screen a bit to hide where the keyboard was. 当键盘被解除时,我稍微滚动屏幕以隐藏键盘的位置。 My problem is that when I use UITapGestureRecognizer to determine if the screen was tapped, the tap doesn't go through to the other controls on the screen. 我的问题是,当我使用UITapGestureRecognizer来确定屏幕是否被点击时,点击不会通过屏幕上的其他控件。 I am using gestureRecognizer.cancelsTouchesInView = NO, it's a problem with the timing. 我正在使用gestureRecognizer.cancelsTouchesInView = NO,这是时间问题。 The screen scrolls away before the control recognizes that it was clicked. 屏幕在控件识别出被单击之前滚动。 Any idea how I can solve the problem? 知道如何解决这个问题吗? I'm perfectly happy using something other than gesture recognition. 我非常高兴使用手势识别之外的其他东西。

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

Keep the gesture recognizer. 保持手势识别器。 Have it use a method like this: 让它使用这样的方法:

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

I found the solution to the problem. 我找到了问题的解决方案。 I'm still using the UITapGestureRecognizer, but I now have a zero-length delay before hiding the keyboard, which allows the tap event to propagate correctly to the subviews, such as buttons. 我仍在使用UITapGestureRecognizer,但我现在在隐藏键盘之前有一个零长度延迟,这允许点击事件正确传播到子视图,例如按钮。 The basic view is a text field and button controls filling the screen. 基本视图是填充屏幕的文本字段和按钮控件。 To allow the screen to be viewed correctly when the keyboard is shown, the whole thing is wrapped in a scroll view, and a placeholder view for the area the keyboard takes up is added to the bottom. 为了在显示键盘时正确查看屏幕,整个内容都包含在滚动视图中,键盘占用区域的占位符视图将添加到底部。 It is only expanded when the keyboard is shown. 它仅在显示键盘时展开。 Here are all the relevant pieces of code, which should allow anyone to implement the tap-anywhere-to-dismiss-keyboard idea as well as solving the problem of controls being hidden by the keyboard: 以下是所有相关的代码片段,应该允许任何人实现tap-anywhere-to-dismiss-keyboard的想法,以及解决键盘隐藏的控件问题:

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

Hope someone finds this useful. 希望有人觉得这很有用。

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

相关问题 我如何知道何时点击UIKeyboard Join按钮? - how do I know when UIKeyboard Join button is tapped? 轻按Web视图时如何隐藏导航视图? [苹果手机] - How do I hide the navigation view when the webview is tapped? [iphone] 在iOS中点击时如何更改UITableViewHeader背景颜色 - How to change UITableViewHeader background color when tapped in iOS 使用新通知机制点击本地通知时如何打开应用程序? - How do I open the app when local notification is tapped with the new notification mechanisms? 如何确定我的应用程序何时即将在后台终止? - How can I determine when my application is about to be terminated in the background? 如何确定滚动视图中当前可见的区域并确定中心? - How can I determine the area currently visible in a scrollview and determine the center? 如何捕获最初在 UIPanGestureRecognizer 中点击的点? - How do I capture the point initially tapped in a UIPanGestureRecognizer? 如何确定是否启动应用程序以执行后台提取 - How to determine if application is launched to do Background Fetch 如何使用 Jqtouch 检测何时点击按钮? - How can i detect when a button is tapped using Jqtouch? iPhone,我如何确定子屏幕何时传递回UITableView以及如何更新该行? - iPhone, how do I determine when a child screen passes back to a UITableView and how do I update that row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM