为什么要按顺序给 UIView 变换属性赋值?
在阅读示例代码时,我发现了一些关于方向改变的代码。有趣的是,self.view 的 transform 属性是按顺序赋值的。从逻辑上讲,第一个赋值似乎没有任何作用,因为它被后面的赋值覆盖了。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationPortrait == toInterfaceOrientation)
{
self.view = portraitView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0, 0, 320, 480);
} else if (UIInterfaceOrientationLandscapeLeft == toInterfaceOrientation) {
self.view = landscapeView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
} else {
self.view = landscapeView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
}
}
该文档说方法 willAnimateRotationToInterfaceOrientation 在方向动画实际发生之前被调用。
那么,对 self.view.transform 的赋值实际上就像将值推入堆栈一样?或者 Cocoa Touch 如何知道视图应该首先设置为 CGAffineTransfrmIndentity 然后设置为另一个值?
While reading sample code, I found some code about orientation change. The interesting part is that transform property of self.view is assigned with value in sequence. Logically, it seems the first assignment doesn't take any effect since it is overwritten by following assignment.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationPortrait == toInterfaceOrientation)
{
self.view = portraitView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0, 0, 320, 480);
} else if (UIInterfaceOrientationLandscapeLeft == toInterfaceOrientation) {
self.view = landscapeView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
} else {
self.view = landscapeView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
}
}
The doc says the method willAnimateRotationToInterfaceOrientation is called before orientation animation actually takes place.
So, the assignment to self.view.transform is actually working like pushing value to a stack? or how does Cocoa Touch know that the view should be first set to CGAffineTransfrmIndentity then to another value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,身份转换本身不会做任何事情。所以这里的双重分配是没有意义的。
顺便说一句:Apple 的示例仅使用单一分配:
GKTank 示例
Well, identity transform doesn't do anything on its own. So double assignments here are pointless.
BTW: Samples from Apple use only single assignments:
GKTank sample