问题
Question
I have a UIView
and I apply to it a CGAffineTransform
. I would like to be able to know the frame of the view if the transform had not be applied.
I am looking for a (simple) solution that handles the case where the transform is the "zero-transform" (which is not invertible).
My attempt
The following does not work for the "zero-transform", I guess.
CGRect result ;
CGAffineTransform transform = self.transform ;
result = CGRectApplyAffineTransform(self.frame, CGAffineTransformInvert(transform)) ;
return result ;
Why?
I need to do this for an animation:
- initial settings: I set the
transform
"to 0". The view is inserted but the frames of all the elements do as if the view was not here. - final settings: I compute the frames of all my elements, and this time I take into account the new view. I set its
transform
"to 1".
Remarks
My transform is a scale transform.
回答1:
transform
does not affect center
nor bounds
.
So, you can calculate it by this simple code:
CGPoint center = view.center;
CGSize size = view.bounds.size;
CGRect frameForTransformIdentity = CGRectMake(
center.x - size.width / 2,
center.y - size.height / 2,
size.width,
size.height
);
回答2:
Swift 4.1 version:
extension UIView
{
var frameWithoutTransform: CGRect
{
let center = self.center
let size = self.bounds.size
return CGRect(x: center.x - size.width / 2,
y: center.y - size.height / 2,
width: size.width,
height: size.height)
}
}
来源:https://stackoverflow.com/questions/26192588/how-to-compute-the-frame-of-a-uiview-before-the-transform