[英]How do I determine when the background area is tapped?
我試圖確定用戶何時點擊屏幕上的單個UITextView以外的某個區域。 這與關於UITableViews的這個問題類似,但我在那里提出的解決方案存在一些問題。 當鍵盤被解除時,我稍微滾動屏幕以隱藏鍵盤的位置。 我的問題是,當我使用UITapGestureRecognizer來確定屏幕是否被點擊時,點擊不會通過屏幕上的其他控件。 我正在使用gestureRecognizer.cancelsTouchesInView = NO,這是時間問題。 屏幕在控件識別出被單擊之前滾動。 知道如何解決這個問題嗎? 我非常高興使用手勢識別之外的其他東西。
我曾經使用自定義(不可見)按鈕作為背景圖層來執行此操作。
保持手勢識別器。 讓它使用這樣的方法:
- (void)dismissKeyboard:(UIGestureRecognizer *)gesture
{
[self.view endEditing:NO];
}
我找到了問題的解決方案。 我仍在使用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];
}
希望有人覺得這很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.