[英]How to understand frames for UIViews with rotation transforms applied?
我在執行下面的代碼時遇到了一個奇怪的問題,因為UIView應用了旋轉變換。 我要做的是從superview中刪除視圖,然后將其插入特定視圖並恢復其框架:
UIView* movedView= [self.views objectAtIndex:newIndex];
center = movedView.center;
CGRect oldFrame = movedView.frame;
//find the layer on top and insert the moved view under that
UIView* coveringView = [self.views objectAtIndex:newIndex-1];
[movedView removeFromSuperview];
[self.view insertSubview:movedView belowSubview:coveringView];
movedView.frame = oldFrame;
我希望視圖在操作后出現在同一個位置,但它會根據通過CGAffineTransformMakeRotation()應用的旋轉量而移動。
通過將分配幀的調用替換為分配中心屬性的調用,我得到了所需的行為。 這讓我思考 - 我怎么能理解旋轉時UIView框架發生了什么? 是否有一個很好的教程或某些東西可以幫助我在概念上理解變換如何影響UIView的框架?
-[UIView frame]
文檔說明了這一點:
警告:如果
transform
屬性不是identity變換,則此屬性的值未定義,因此應忽略。
和這個:
可以對此屬性的更改進行動畫處理。 但是,如果
transform
屬性包含非標識變換,則frame
屬性的值是未定義的,不應修改。 在這種情況下,您可以使用center
屬性重新定位視圖,並使用bounds
屬性調整大小。
所以你可能想要完全避免考慮frame
屬性,因為你的transform
不是身份。
在CGAffineTransform
,不再定義UIView
的屬性frame
。 文檔說“警告:如果轉換屬性不是標識轉換,則此屬性的值是未定義的,因此應該被忽略。”
在我看來, frame
是由center
, transform
和bounds
就像公式: frame = f(center,transform,bounds)
。 但我不知道f(x)
的內部
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.