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

时间:2011-02-28 23:28:56

标签: iphone cocoa-touch ios uiscrollview

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

3 个答案:

答案 0 :(得分:2)

我过去曾使用自定义(不可见)按钮作为背景图层。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我找到了问题的解决方案。我仍在使用UITapGestureRecognizer,但我现在在隐藏键盘之前有一个零长度延迟,这允许点击事件正确传播到子视图,例如按钮。基本视图是填充屏幕的文本字段和按钮控件。为了在显示键盘时正确查看屏幕,整个内容都包含在滚动视图中,键盘占用区域的占位符视图将添加到底部。它仅在显示键盘时展开。以下是所有相关的代码片段,应该允许任何人实现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];
}

希望有人觉得这很有用。

相关问题