我有一个自定义 UIView
self.commentView = [[[NSBundle mainBundle] loadNibNamed"commentView" owner:self options:nil] firstObject];
self.commentView.userInteractionEnabled = YES;
CGRect frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height, [[UIScreen mainScreen]bounds].size.width, 52);
[self.commentView setFrame:frame];
[self addSubview:self.commentView];
添加手势识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self actionselector(tapping)];
[self.commentView addGestureRecognizer:tap];
自定义 View 包含一个 UITextView 。我注册键盘通知并在键盘出现时更改我的自定义 View 框架
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardAppeared
name:UIKeyboardDidShowNotification
object:nil];
- (void)keyboardAppearedNSNotification*)notificationn{
NSDictionary* keyboardInfo = [notificationn userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
CGRect frame = self.commentView.frame;
frame.origin.y = (keyboardFrameBeginRect.origin.y - frame.size.height);
[UIView animateWithDuration:.1f animations:^{
[self.commentView setFrame:frame];
self.commentView.alpha = 1.0f;
}];
keyboard = keyboardFrameBeginRect.origin.y;
}
在我的“点击”方法中,我有一个 NSLog 。当我最初添加 commentView 并点击 View 时,触摸工作并调用 tapping 。在显示键盘并将 commentView 的框架移到键盘上方后,不再调用“轻敲” - 触摸事件停止。
我还有一个关于何时关闭键盘的通知。在这种方法中,我将框架设置回原来的样子。触摸再次开始起作用。
编辑
这就是我添加 commentView 的方式。上面的 addSubview: 正在测试:
- (void) showCommentView{
CGRect frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height-52, [[UIScreen mainScreen]bounds].size.width, 52);
[self.commentView setFrame:frame];
if (![self.subviews containsObject:self.commentView]) {
[self addSubview:self.commentView];
}
[self.commentView.commentTextView becomeFirstResponder];
CGRect framer = [self convertRect:self.commentView.postButton.frame fromView:self.commentView];
NSLog(@"SUPERVIEW: %f", framer.origin.y);
}
我注释掉 commentTextView becomeFirstResponder 行,然后点击就可以了。如果我离开这一行,将调用 keyboardAppeared ,并且一旦框架更改,点击就不再起作用。所以我认为 commentView 的框架实际上并没有改变。
Best Answer-推荐答案 strong>
设置commentView 的color 。我认为当键盘出现时您的commentView 框架没有正确调整大小。
关于ios - 更改 UIView 框架结束触摸事件,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/38258055/
|