简体   繁体   English

在UITableView上移动UIView - 触摸顶部UIView仍然选择表行

[英]Moving a UIView on top of UITableView - touching the top UIView still selects table rows

I have a UIView which contains three containers 我有一个包含三个容器的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---- |  |
|     | -----------   |
----------------------

An event happens where I shift the frame of Container B (and I show a view (labeled O) to the right of B, which isn't important). 一个事件发生在我移动容器B的框架(我在B的右边显示一个视图(标记为O),这并不重要)。 Now Container B is overlapping Container A 现在容器B与容器A重叠

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

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

But now, when I try to select a row in Container C's UITableView by touching the left edge of the row (@), the touch is being registered by Container A's UITableView. 但现在,当我尝试通过触摸行的左边缘(@)在Container C的UITableView中选择一行时,Container A的UITableView正在注册触摸。 Or even if I touch on the view above the table (*), the corresponding row in A is selected. 或者即使我触摸表格(*)上方的视图,也会选择A中的相应行。

I can solve part of the problem by changing the width of the Container A's UITableView. 我可以通过改变容器A的UITableView的宽度来解决部分问题。 But then I still have the problem where if I touch table C (@), the C's row doesn't select. 但是我仍然有问题,如果我触摸表C(@),C的行不选择。 It's like it things the start of the table C is in the old location ($). 这就像表C的起点位于旧位置($)。

So what can I do so that tapping at the new @ location will select the correct row in C? 那么我该怎么办才能点击新的@位置将选择C中的正确行?

======= =======

EDIT with some code: 用一些代码编辑

This is code I do in Container B. It's a very straight forward animation of B's frame. 这是我在容器B中所做的代码。这是B帧的一个非常直接的动画。 self.view is Container B's UIView. self.view是Container B的UIView。

All the view's are on the screen (the "other" container is hidden) through the Storyboard. 所有视图都在屏幕上(隐藏“其他”容器)通过故事板。 Other container is a subview of B. 其他容器是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];
                 }
];

Please let me know what other code you want to see. 请让我知道你想看到的其他代码。

Mmm... Not sure if this works in your case, but try to manage your animation in a bigger container that hold all your other container. 嗯......不确定这是否适用于您的情况,但尝试在容纳所有其他容器的更大容器中管理您的动画。

I mean, create a Container Z that contains A, B, O and their subviews and try to animate the position of B from Z, checking that B is in front of A. 我的意思是,创建一个包含A,B,O及其子视图的Container Z,并尝试从Z中设置B的位置动画,检查B是否在A前面。

So I downloaded Reveal to visually see (in 3d) what was happening with the views. 所以我下载了Reveal来直观地看到(在3d中)视图中发生了什么。 It looks like the diagrams I drew are not complete, and misleading. 看起来我绘制的图表并不完整,并且具有误导性。

Each of the containers A and B (which are UIViews) contain subviews A' and B' (also UIViews). 容器A和B(UIViews)中的每一个都包含子视图A'和B'(也是UIViews)。 In the animation block when I was changing self.view, I was really moving the frame for B'. 在我改变self.view的动画块中,我真的在移动B'的框架。 The frame was moving out of the bounds of B. 框架正在移出B.的范围。

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

So even the B' was overlapping A and A', I suppose in the hierarchy of views, the touch is still captured by the underlying subviews (containers) A and B. I don't fully think this is how it should happen, but I guess that's how things fall. 因此,即使B'重叠A和A',我认为在视图层次结构中,触摸仍然被底层子视图(容器)A和B捕获。我不完全认为它是如何发生的,但是我猜这就是事情的结果。

Anyway, the solution was simple. 无论如何,解决方案很简单。

instead of: 代替:

[self.view setFrame:frame];

I'm essentially going to do: 我基本上会这样做:

[self.view.superview setFrame:frame];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM