【问题标题】:Changing UIView frame ends touching events更改 UIView 框架结束触摸事件
【发布时间】:2023-03-21 18:45:02
【问题描述】:

我有一个自定义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 action:@selector(tapping)];
[self.commentView addGestureRecognizer:tap];

自定义视图包含UITextView。我注册键盘通知并在键盘出现时更改我的自定义视图框架

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardAppeared:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

 - (void)keyboardAppeared:(NSNotification*)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 并点击视图时,触摸工作并调用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的框架实际上并没有改变。

【问题讨论】:

  • 您使用的是模拟器吗?如果你是,你实际上可以通过使用颜色混合层来检查你是否正在点击评论视图。可能您正在点击差异视图,这就是手势无法识别的原因?
  • @Joshua 不使用模拟器。将视图设置为不同的颜色以及bringSubviewToFront。很确定我正在点击正确的视图
  • 不想改变.y值你应该改变textview的高度

标签: ios objective-c uiview touch-event


【解决方案1】:

设置commentViewcolor。我认为当键盘出现时,您的 commentView 框架没有正确调整大小。

【讨论】:

  • 视图的backgroundColor 是亮橙色。我想你是正确的。视图似乎在键盘上方移动,但在其框架实际上没有改变的地方发生了一些奇怪的事情。
猜你喜欢
  • 1970-01-01
  • 2012-10-26
  • 2014-06-24
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 2014-04-26
  • 1970-01-01
相关资源
最近更新 更多