为什么要按顺序给 UIView 变换属性赋值?

发布于 2024-12-23 09:38:43 字数 1344 浏览 3 评论 0原文

在阅读示例代码时,我发现了一些关于方向改变的代码。有趣的是,self.viewtransform 属性是按顺序赋值的。从逻辑上讲,第一个赋值似乎没有任何作用,因为它被后面的赋值覆盖了。

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在我安 2024-12-30 09:38:44

嗯,身份转换本身不会做任何事情。所以这里的双重分配是没有意义的。

顺便说一句: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文