[英]When moving a subview around, how do I keep a subview of it in the same place in its superview?
That's a very convoluted title. 这是一个非常令人费解的标题。 Sorry.
抱歉。
Basically, I have a UIView
that I've attached to a UITableViewCell
so I can slide the cell around, or at least create the tric-- illusion of it. 基本上,我有一个
UIView
,我已经附加到UITableViewCell
所以我可以滑动单元格,或至少创建它的幻觉 - 幻觉。 Within that UIView
I have a UIImageView
. 在
UIView
我有一个UIImageView
。 I want to keep this UIImageView
static relative to the view controller . 我想保持这个
UIImageView
相对于视图控制器静态。 So basically when I slide the cell around, I want everything about the cell to slide around with my finger (I have this done) EXCEPT the UIImageView
. 所以基本上当我滑动单元格时,我希望用手指滑动单元格的所有内容(我已完成)除了
UIImageView
。 I want it to seem to the user like it's staying in one place -- which means to the cell's superview (the view controller's view) it's staying in one place. 我希望用户觉得它似乎停留在一个地方 - 这意味着单元格的超级视图(视图控制器的视图)它停留在一个地方。
What would be the easiest way to build this? 构建这个最简单的方法是什么? I'm sorry I don't have code to share, I'm just trying to build this from a conceptual level first.
对不起,我没有代码要分享,我只想先从概念层面构建这个代码。 My
UIView
on the UITableViewCell
is slid with a UIPanGestureRecognizer
and moving the UIView
's frame if that helps. 我在
UITableViewCell
上的UIView
滑动了一个UIPanGestureRecognizer
并移动了UIView
的帧,如果这有帮助的话。
Would I have to move it in the opposite direction of the UIView
when it moves (say it moves one forward in x, move it one back in x?) or is there a way where I can just say "lock your position relative to your super-superview"? 当
UIView
移动时,我是否必须将它向UIView
的相反方向移动(比如它在x中向前移动一个,在x中向后移动一个?)或者有一种方法可以说“锁定相对于你的位置”超上海华“?
I've had the same problem before.. what I did was simply not add the UIImageView
as a subview to the UITableViewCell
, and instead attach it to the parent of the UITableViewCell
.. that way whatever happens to the UITableViewCell
frame properties (ie size or origin) won't impact your UIImageView
.. 我之前遇到过同样的问题..我所做的只是不将
UIImageView
作为子视图添加到UITableViewCell
,而是将其附加到UITableViewCell
的父级..这样就可以了解UITableViewCell
框架属性(即大小)或来源)不会影响你的UIImageView
..
you may wonder: well one of the reasons I attach the UIImageView as a subview to the tableCell is b/c it's related to it in many different ways.. The response to that is that you can associate an object with another through objc_setAssociatedObject
您可能想知道:我将UIImageView作为子视图附加到tableCell的原因之一是b / c它以多种不同的方式与它相关。对此的响应是您可以通过
objc_setAssociatedObject
将对象与另一个对象关联
If you come from web dev background, you can think of this discussion as the difference between position:absolute
and position:relative
discussion. 如果您来自web dev背景,您可以将此讨论视为
position:absolute
和position:relative
之间的区别position:relative
讨论。
Initially I tried to do what you had in mind, basically moving them in opposite directions etc.. I just think my solution is a lot simpler to do. 最初我试图做你想到的事情,基本上是朝着相反的方向移动它们。我只是认为我的解决方案要简单得多。
abood was close, but close, but if you simply make the imageview a subview of the cell itself instead of the UIView I added over the cell, they operate independently. abood很接近,但接近,但是如果你只是让imageview成为单元格本身的子视图而不是我在单元格上添加的UIView,它们就会独立运行。 So contrary to the first line, adding it as a subview of UITableViewCell worked perfectly and didn't have any of the issues outlined.
因此,与第一行相反,将其添加为UITableViewCell的子视图非常有效,并且没有列出任何问题。 Works perfectly now.
现在工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.