Obj-C - 通过从 UITextView 点击屏幕上的任意位置来关闭键盘?
Obj-C - Dismiss keyboard by tapping anywhere on screen from UITextView?
当我的用户点击我的 UITextView(不是 uitextfield...)时,会出现动画。然而,在 UITextView 开始编辑之后,似乎没有其他 UITapGesture 被识别(例如,如果我将 tapGesture 添加到我的 UIView 以关闭此动画,它根本不会执行)。我一直在努力解决这个问题,直到永远。感谢帮助,我很难过。
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.replyField.delegate = self;
[self.replyField setUserInteractionEnabled:YES];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
-(void)textViewTapped {
NSLog(@"DISMISS PLEASE!");
[self animateTextView:NO];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextView: YES];
self.replyField.gestureRecognizers = nil;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[self animateTextView:NO];
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance = 206;
const float movementDuration = 0.3f;
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
[UIView commitAnimations];
}
- (void)textViewDidChange:(UITextView *)textView
{
}
在 viewController 你可以这样做
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:Yes];
}
在 ViewDidLoad 中:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
创建一个方法:
-(void)dismissKeyboard {
[textView resignFirstResponder];
}
我遇到了类似的问题,使用下面的代码通过点击屏幕视图上的任意位置来关闭键盘。希望对你有所帮助。
Objective c
- (void)viewDidLoad
{
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
}
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
Swift 4
func viewDidLoad() {
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(gestureRecognizer)
gestureRecognizer.cancelsTouchesInView = false
}
func dismissKeyboard() {
view.endEditing(true)
}
不太明白你想要什么顺便说一句让我解释一下。
首先,如果你想隐藏你的键盘,你可以通过一行代码来实现。
Objective C
[self.view endEditing:YES];
Swift
view.endEditing(true)
现在在您的代码中,您在 self.view
上添加了点击手势,这意味着如果您在任何地方触摸主视图,那么您的手势方法将被调用。如果你希望我的键盘在我触摸主视图时隐藏,那么在点击手势的方法中你应该写上面的代码 ([self.view endEditing:YES];
) 但是确保键盘打开如果你点击你的 textView。
但是如果你希望键盘在我点击 UITextView
时不打开,那么有很多方法,但请按照下面的步骤操作
Objective C
UIView* dummyInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextView.inputView = dummyInputView;
Swift
let dummyInputView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
myTextView.inputView = dummyInputView
这里键盘不会打开但是光标会闪烁如果你想隐藏光标那么你可以通过下面的代码来实现
Objective C
myTextView.tintColor = [UIColor clearColor];
Swift
myTextView.tintColor = UIColor.clear
如果您有任何问题,请发表评论。
您的问题与手势识别器无关。
线路引起的问题:
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
您从 self.inputView 为 self.view 设置了偏移量的框架。
并且当 self.view 大小改变时,其子视图(包括文本视图)的位置可能超出父视图范围。如果触摸点超出父视图范围,UIView 不会响应触摸事件。
您可以通过为视图设置背景颜色并在日志中显示它们的框架来轻松检查这一点:
...
- (void) animateTextView:(BOOL) up
{
NSLog(@"self.view state1:%@", self.view);
NSLog(@"self.replyField state1:%@", self.replyField);
self.view.backgroundColor = [UIColor redColor];
self.replyField.backgroundColor = [UIColor blueColor];
const int movementDistance = 206;
const float movementDuration = 0.3f;
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
//self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
self.replyField.frame = CGRectOffset(self.replyField.frame, 0, movement);
[UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
[UIView commitAnimations];
}
- (void)afterAnimationStops {
NSLog(@"self.view state2:%@", self.view);
NSLog(@"self.replyField state2:%@", self.replyField);
}
...
还有什么是self.inputView?是否设置正确?
顺便说一句:代码的目标是什么?你想移动文本视图以避免键盘重叠吗?然后考虑放在 UIScrollView 中。
例如,参见此处的讨论:How to make a UITextField move up when keyboard is present?.
当我的用户点击我的 UITextView(不是 uitextfield...)时,会出现动画。然而,在 UITextView 开始编辑之后,似乎没有其他 UITapGesture 被识别(例如,如果我将 tapGesture 添加到我的 UIView 以关闭此动画,它根本不会执行)。我一直在努力解决这个问题,直到永远。感谢帮助,我很难过。
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.replyField.delegate = self;
[self.replyField setUserInteractionEnabled:YES];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
-(void)textViewTapped {
NSLog(@"DISMISS PLEASE!");
[self animateTextView:NO];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextView: YES];
self.replyField.gestureRecognizers = nil;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[self animateTextView:NO];
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance = 206;
const float movementDuration = 0.3f;
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
[UIView commitAnimations];
}
- (void)textViewDidChange:(UITextView *)textView
{
}
在 viewController 你可以这样做
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:Yes];
}
在 ViewDidLoad 中:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
创建一个方法:
-(void)dismissKeyboard {
[textView resignFirstResponder];
}
我遇到了类似的问题,使用下面的代码通过点击屏幕视图上的任意位置来关闭键盘。希望对你有所帮助。
Objective c
- (void)viewDidLoad
{
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
}
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
Swift 4
func viewDidLoad() {
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(gestureRecognizer)
gestureRecognizer.cancelsTouchesInView = false
}
func dismissKeyboard() {
view.endEditing(true)
}
不太明白你想要什么顺便说一句让我解释一下。
首先,如果你想隐藏你的键盘,你可以通过一行代码来实现。
Objective C
[self.view endEditing:YES];
Swift
view.endEditing(true)
现在在您的代码中,您在 self.view
上添加了点击手势,这意味着如果您在任何地方触摸主视图,那么您的手势方法将被调用。如果你希望我的键盘在我触摸主视图时隐藏,那么在点击手势的方法中你应该写上面的代码 ([self.view endEditing:YES];
) 但是确保键盘打开如果你点击你的 textView。
但是如果你希望键盘在我点击 UITextView
时不打开,那么有很多方法,但请按照下面的步骤操作
Objective C
UIView* dummyInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextView.inputView = dummyInputView;
Swift
let dummyInputView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
myTextView.inputView = dummyInputView
这里键盘不会打开但是光标会闪烁如果你想隐藏光标那么你可以通过下面的代码来实现
Objective C
myTextView.tintColor = [UIColor clearColor];
Swift
myTextView.tintColor = UIColor.clear
如果您有任何问题,请发表评论。
您的问题与手势识别器无关。 线路引起的问题:
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
您从 self.inputView 为 self.view 设置了偏移量的框架。
并且当 self.view 大小改变时,其子视图(包括文本视图)的位置可能超出父视图范围。如果触摸点超出父视图范围,UIView 不会响应触摸事件。 您可以通过为视图设置背景颜色并在日志中显示它们的框架来轻松检查这一点:
...
- (void) animateTextView:(BOOL) up
{
NSLog(@"self.view state1:%@", self.view);
NSLog(@"self.replyField state1:%@", self.replyField);
self.view.backgroundColor = [UIColor redColor];
self.replyField.backgroundColor = [UIColor blueColor];
const int movementDistance = 206;
const float movementDuration = 0.3f;
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
//self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
self.replyField.frame = CGRectOffset(self.replyField.frame, 0, movement);
[UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
[UIView commitAnimations];
}
- (void)afterAnimationStops {
NSLog(@"self.view state2:%@", self.view);
NSLog(@"self.replyField state2:%@", self.replyField);
}
...
还有什么是self.inputView?是否设置正确?
顺便说一句:代码的目标是什么?你想移动文本视图以避免键盘重叠吗?然后考虑放在 UIScrollView 中。 例如,参见此处的讨论:How to make a UITextField move up when keyboard is present?.