简体   繁体   English

移动子视图时,如何在其超视图中将其子视图保留在同一位置?

[英]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:absoluteposition: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.

相关问题 子视图不随其父视图一起移动 - Subview not moving along with its superview 如何添加具有约束的子视图以使子视图具有与其父视图相同的框架? - How to add a subview with constraints to make the subview with the same frame as its superview? 在superView中将containerView作为子视图移动 - Moving containerView in superView as a subview 如何使超级视图自动调整大小并与其子视图保持一致? - How to make a superview resize automatically same size and coordinate with its subview? 移动子视图时移动超级视图-ObjectiveC - Move superview when moving subview - ObjectiveC 当用户在iPhone中触摸其子视图时,如何实现关闭子视图? - How to implement close a subview when user touched its superview in iphone? 当其父视图更改大小时,如何告诉子视图调整大小? - How can I tell a subview to resize when its superview changes size? 当两个子视图具有相同的父视图时,根据其他子视图进行居中 - center subview according to other subview when both have the same superview 如何将子视图大小调整传播到superview大小调整? - How do I propagate subview resizing to superview resizing? 如何从超级视图的超级视图中将subView置于顶部 - How to bring subView to top from superview of its superview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM