ios - 为什么围绕 x 轴平移 CALayer 会影响围绕 Y 轴的旋转

标签 ios uiimageview rotation core-animation calayer

我添加了一个 CALayer,并按以下方式进行了转换。我假设在旋转之前将图层平移任何常数不会对绕 Y 轴的旋转产生影响,但它似乎有所不同,我在这里缺少什么?

- (void)viewDidAppear:(BOOL)animated
{
    CALayer* layer = [CALayer layer];
    layer.bounds = self.img1.bounds;
    layer.backgroundColor = [[UIColor grayColor] CGColor];
    layer.anchorPoint = CGPointMake(0, 0.5);
    layer.position = CGPointMake(0, CGRectGetMidY(self.view.bounds));
    layer.contents = (id)[self.img1.image CGImage];
    layer.transform = [self transformforView1];
    [self.view.layer addSublayer:layer];
}

没有任何翻译和输出的旋转:

- (CATransform3D)transformforView1
{
    CATransform3D t = CATransform3DIdentity;
    t.m34 = 1.0/-700;
    t = CATransform3DTranslate(t, 0, 0, 0);
    t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
    return t;
}

Rotation without any translation and output

相同的旋转,平移 500 并输出:

- (CATransform3D)transformforView1
{
    CATransform3D t = CATransform3DIdentity;
    t.m34 = 1.0/-700;
    t = CATransform3DTranslate(t, 500, 0, 0);
    t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
    return t;
}

Same rotation with translation of 500 and output

最佳答案

这其实是一个非常好的问题。造成困惑的原因是模型 View 矩阵和投影矩阵通常是分开的。

阅读此问题以供引用:

The purpose of Model View Projection Matrix

我建议您将模型 View 矩阵和投影矩阵分开。

因此,您的 CATransform3D t 实际上是两个矩阵的串联,您可以添加另一个转换以在 View 中移动模型,或者然后创建另一个转换(您的模型 View 矩阵)。这就是我在这里所做的:

CATransform3D modelViewTransform = CATransform3DMakeTranslation([UIScreen mainScreen].bounds.size.height / 2.0, 0, 0);
CATransform3D t = CATransform3DIdentity;
t.m34 = 1.0/-700;
t = CATransform3DTranslate(t, 500, 0, 0);
t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
return CATransform3DConcat(t, modelViewTransform);

这会将您的模型移动到屏幕中心,而不是屏幕边缘。

关于ios - 为什么围绕 x 轴平移 CALayer 会影响围绕 Y 轴的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183442/

相关文章:

ios - 围绕其中心旋转 UIView 保持其大小

ios - 将从 Web 服务检索的项目存储为 Core Data 对象是一种好的做法吗?

ios - 在 WkWebView/mobile Safari 的页面中为 iframe 启用 session cookie

swift - 在表格 View 中的单元格内自动布局图像。布局正确,但只能向下和向后滚动一次?

iphone - UIImageView 在矩形中绘制图像大小而不是 ImageView

iphone - 为什么 UIImage 不能正确拉伸(stretch)?

java - Box2D 远离行星旋转

javascript - 如何用JavaScript访问CSS3的 "transform"属性?

ios - 如何为一定数量的设备分发 iOS 应用程序?

ios - 如何在 iTunes Connect 地区选择孟加拉国国家?