简体   繁体   中英

Wrong frame after adding transformed view to UIWindow or UIView

The following code will break the frame (which will be enormous), as seen in iOS 8.1.3.

someView.layer.transform = CGAffineTransformMakeScale(0.001f, 0.001f);
[[[[UIApplication sharedApplication] keyWindow].subviews lastObject] addSubview:someView];

[UIView animateWithDuration:0.3f animations:^{
    someView.transform = CATransform3DIdentity;
}];

Removing the transformation and replacing it with a simple frame movement animation works well. Why?

After some investigation and reasoning I came to the conclusion that it might be a good idea to apply the transformation of someView after it has been added to a view. And this turned out to be the solution. So we write:

[[[[UIApplication sharedApplication] keyWindow].subviews lastObject] addSubview:someView];
someView.layer.transform = CGAffineTransformMakeScale(0.001f, 0.001f);

[UIView animateWithDuration:0.3f animations:^{
    someView.transform = CGAffineTransformIdentity;
}];

Which works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM