【问题标题】:Moving a UIView on top of UITableView - touching the top UIView still selects table rows在 UITableView 上移动 UIView - 触摸顶部 UIView 仍会选择表格行
【发布时间】:2013-08-08 01:06:09
【问题描述】:

我有一个包含三个容器的 UIView

Container A - contains a UITableView
Container B - contains Container C and Container O
Container C - contains a UITableView
* - touch event
@ - touch event

----------------------
|     |               |
| ---*|      B        |
| --- |               |
| -A- | -----------   |
| --- ||@----C---- |  |
|     | -----------   |
----------------------

在我移动容器 B 的框架时发生了一个事件(并且我在 B 的右侧显示了一个视图(标记为 O),这并不重要)。现在容器 B 与容器 A 重叠

O - Other view (unimportant)
$ - original location of touch @

----------------------
|   |               | | 
| --|*     B        | |
| --|               | |
| -A|------------   |O|
| --||@-$--C----|   | |
|   |------------   | |
----------------------

但是现在,当我尝试通过触摸行的左边缘 (@) 来选择容器 C 的 UITableView 中的一行时,容器 A 的 UITableView 正在注册触摸。或者即使我触摸表格上方的视图 (*),也会选择 A 中的相应行。

我可以通过改变Container A的UITableView的宽度来解决部分问题。但是我仍然有一个问题,如果我触摸表 C (@),C 的行不会选择。就像表 C 的开头位于旧位置 ($) 一样。

那么我该怎么做才能在新的@位置点击将在 C 中选择正确的行?

=======

编辑一些代码:

这是我在容器 B 中编写的代码。它是 B 帧的非常直接的动画。 self.view 是 Container B 的 UIView。

所有视图都通过情节提要显示在屏幕上(“其他”容器被隐藏)。其他容器是 B 的子视图。

// this code basically aligns the "other" container to the right/"offscreen" of 
// Container B. Yes extending beyond the frame of B, but this part works fine
CGRect otherContainerFrame = self.otherContainerView.frame;
otherContainerFrame.origin.x = CGRectGetMaxX(self.view.frame);
[self.otherContainerView setFrame:otherContainerFrame];
[self.otherContainerView setHidden:NO];

// I'm going move Container B by the width of the "other" view
float offset = CGRectGetWidth(self.otherContainerView.frame);

CGRect frame = self.view.frame;
frame.origin.x -= offset;
[UIView animateWithDuration:.35
                 animations:^{
                     [self.view setFrame:frame];
                 }
];

请让我知道您还想看什么其他代码。

【问题讨论】:

  • 很好的解释,但我认为我们需要查看用于显示和动画容器的代码来解决这个问题...
  • @Beppe,我为动画添加了代码。

标签: ios objective-c cocoa-touch uitableview uiview


【解决方案1】:

嗯...不确定这是否适用于您的情况,但请尝试在容纳所有其他容器的更大容器中管理您的动画。

我的意思是,创建一个包含 A、B、O 及其子视图的容器 Z,并尝试从 Z 为 B 的位置设置动画,检查 B 是否在 A 前面。

【讨论】:

  • 感谢您的建议。我把逻辑移到了A和B的父ViewController,同样的行为存在。
【解决方案2】:

所以我下载了 Reveal 以直观地(在 3d 中)查看视图发生了什么。看来我画的图不完整,有误导性。

每个容器 A 和 B(它们是 UIView)都包含子视图 A' 和 B'(也是 UIView)。在我改变 self.view 的动画块中,我真的在为 B' 移动框架。框架正在移动到 B 的范围之外。

----------------------
|  A' |      B'       |
| ---*|      B        |
| --- |               |
| -A- | -----------   |
| --- ||@----C---- |  |
|     | -----------   |
----------------------

所以即使 B' 与 A 和 A' 重叠,我想在视图的层次结构中,触摸仍然被底层子视图(容器)A 和 B 捕获。我不完全认为这是应该的发生了,但我想事情就是这样发展的。

不管怎样,解决方法很简单。

代替:

[self.view setFrame:frame];

我基本上打算这样做:

[self.view.superview setFrame:frame];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多