Preventing Subview from Transforming when its Superview Transforms

假如想象 提交于 2019-12-23 02:58:06

问题


I have the following set up. I have a UIView called parentView that holds a UIView called childView. parentView can be rotated and resized using some touch gestures which need to be on the parentView. childView has some drawing code in its drawRect method which needs to updated as the parentView is transformed.

I am noticing when I transform the parentView (scale or rotate), childView automatically gets transformed as well. Problem with this is, while I do want the rotate transform to be initiated on the childView, I don't want it to be scale, I'd rather execute its drawing code.

So my question is, what is a good way to handle this situation? I notice when I call "setNeedsDisplay" on my childView after parentView is transformed, it doesn't get executed.


回答1:


Don't transform the parent view; transform a sibling of the child view.

In other words, your current set up looks like this:

transformedView
    childView

Instead, do this:

parentView
    transformedView
    childView

That way the transformation won't affect the child.




回答2:


I had to apply the rotation to the parentView, and then set the bounds property of parentView and childView to resize it. Then I was able to redraw the childView in drawRect successfully. Bottom line is though I think if you set a transform on a childs superview, I think you're stuck having to live with it in its subviews. I tried setting the transforms separately and all of the timing of the transforms got screwed up. The above situation from Brent might also come in handy for certain situations.



来源:https://stackoverflow.com/questions/15582495/preventing-subview-from-transforming-when-its-superview-transforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!