【问题标题】:CGAffineTransformMakeRotation and Autolayout issues under iOS8iOS 8 下 CGAffineTransform Make Rotation 和 Auto Layout 问题
【发布时间】:2014-08-12 10:21:41
【问题描述】:

在简单的 UIView 上应用旋转变换后

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * -0.5);

simpleVIew_.transform = trans;

有以下限制

[self addConstraints:@[
   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
                                   toItem:nil attribute:NSLayoutAttributeNotAnAttribute
                               multiplier:1 constant:50],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeHeight
                               multiplier:1 constant:270],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeLeft
                               multiplier:1.0 constant:0],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeBottom
                               multiplier:1.0 constant:0]

]];

我在 iOS7.1 和 iOS8 beta 5 之间得到了两个不同的结果:

iOS7.1

<UIView: 0x1655d990; frame = (0 30; 50 270); transform = [0, -1, 1, 0, 0, 0]; opaque = NO; layer = <CALayer: 0x1655dab0>

iOS8 beta 5

<UIView: 0x16e5a530; frame = (-110 140; 270 50); transform = [0, -1, 1, 0, 0, 0]; opaque = NO; layer = <CALayer: 0x16e61880>>

请注意框架中的差异 - 宽度/高度值已切换,x、y 坐标已更改。

知道为什么 7.1 和 8 之间存在如此巨大的差异吗?

【问题讨论】:

  • 我有一个类似的问题,我几乎完全确定这是由于他们如何在 iOS 8 中使所有内容都面向接口(基本上改变了坐标系原点的确定方式),但我没有如果是这样,或者如果 iOS 8 中存在错误,或者其他什么,就完全确定了。现在我已经修改了我的应用程序的一小部分,它使用旋转来使用计算出的 CGRects (ugh)
  • 这个问题有什么解决办法吗?
  • 还没有,需要查看最新的iOS8版本。我会更新的。
  • 您找到解决方案了吗?我没有...赞成你的问题。
  • 还没有,你在 iOS 8 GM 上检查过这个吗?

标签: ios objective-c ios8


【解决方案1】:

不是真正的解决方案,而是更多的解决方法。

我发现使用 CGAffineTransformMakeRotation 在 iOS 8 和 iOS 7 上构建时,顶部、底部、左侧和右侧约束的行为不同。但是,CenterX 和 CenterY 的行为相同。此外,只要您 addConstraint 与您 constraintWithItem 相同的视图,高度和宽度约束的行为将相同。

因此,如果您可以将使用 CGAffineTransformMakeRotation 的视图的约束更改为仅使用高度、宽度、CenterX、CenterY,那么您应该能够在两个 iOS 版本上构建相同的约束。

以下是我自己的代码的一个尝试,无论 iOS 版本如何,它都给了我相同的布局:

// Rotate View 90º
endorseHereLabel.transform = CGAffineTransformMakeRotation(-(M_PI)/2);

[self addSubview:endorseHereLabel];
[self bringSubviewToFront:endorseHereLabel];

// Configure Constraints
[self addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                 attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:lineView1
                                                 attribute:NSLayoutAttributeCenterY
                                                multiplier:1.0
                                                  constant:40.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:lineView1
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:kBottomBuffer]];
[endorseHereLabel addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                             attribute:NSLayoutAttributeHeight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:nil
                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                            multiplier:1.0
                                                              constant:12.0]];
[endorseHereLabel addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:nil
                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                            multiplier:1.0
                                                              constant:250.0]];

【讨论】:

    【解决方案2】:

    尝试将简单视图添加到容器视图中。我至少在两个视图中看到了类似的问题,在这两种情况下,将它们添加到容器视图似乎可以解决问题。

    编辑

    我所说的容器视图,只是将简单视图添加到 UIView,然后将约束和旋转应用到容器。

    【讨论】:

      【解决方案3】:

      Documentation:

      @property(nonatomic) CGAffineTransform 变换

      变换的原点是 center 属性的值,或者 图层的 anchorPoint 属性(如果已更改)。 (使用图层 属性来获取底层核心动画层对象。) 默认值为 CGAffineTransformIdentity。

      您需要在旋转之前设置视图的锚点。

      yourView.layer.anchorPoint = CGPointMake(0.5, 0.5);

      值必须介于 0 和 1 之间。

      Sample here

      可以更改默认值 CGAffineTransformIdentity 吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多