【问题标题】:CATransform3DRotate flip horizontalCATransform3DRotate 水平翻转
【发布时间】:2012-04-27 22:06:51
【问题描述】:

我有一个需要转换的图层。目前我正在使用以下内容:

self.customLayer.transform = CATransform3DRotate(CATransform3DIdentity,M_PI / 2.0f, 0, 0, 1);

这正确地使图层正面朝上,但它也需要水平翻转,因为它是错误的方式。我该如何调整CATransform3DRotate 来做到这一点?

【问题讨论】:

  • 你能解释一下你目前得到的结果吗?
  • 只需要水平翻转,不知道怎么解释!

标签: objective-c ios calayer catransform3d


【解决方案1】:

你需要:

self.customLayer.transform = CATransform3DScale(CATransform3DMakeRotation(M_PI / 2.0f, 0, 0, 1),
                                                -1, 1, 1);

带有 -1 的刻度是翻转。想象一下,您正在水平挤压图像并且超过了零。

【讨论】:

  • 它给了我以下错误:将“int”传递给不兼容类型“CATransform3D”的参数并发出警告:函数 CATransform3DMakeRotate 的隐式声明在 C99 中无效。
  • @NicHubbard - 应该是 CATransform3DMakeRotation 而不是 CATransform3DMakeRotate。 +1
  • @NicHubbard 好的,听起来我们走错了方向。试试CATransform3DScale(/* rotation */, 1, -1, 1)
  • (顺便说一句,'水平翻转'一般是指在x方向,即绕y轴翻转)
【解决方案2】:

由于这里的参数是

CATransform3DScale (CATransform3D t, CGFloat sx, CGFloat sy, CGFloat sz)

如果你想水平翻转,你不应该在 CATransform3DMakeRotation() 中提供任何向量值。相反,您只想控制 x 轴的比例。

通过水平翻转它你应该:

self.transform = CATransform3DScale(CATransform3DMakeRotation(0, 0, 0, 0),
                                        -1, 1, 1);

如果你想把它翻转回原点,你可以这样做:

self.transform = CATransform3DScale(CATransform3DMakeRotation(0, 0, 0, 0),
                                        1, 1, 1);  

补充:
较短的版本将为您节省一项操作。翻转:

self.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);

要恢复正常:

self.transform = CATransform3DMakeRotation(0, 0, 1, 0);

【讨论】:

  • 如何在 CALayer 中添加这个带有动​​画的变换?
【解决方案3】:

我个人觉得使用 KVC 来处理这些事情更容易阅读。您可能可以使用类似以下的方法来达到相同的效果:

// Rotate the layer 90 degrees to the left
[self.customLayer setValue:@-1.5707 forKeyPath:@"transform.rotation"];
// Flip the layer horizontally
[self.customLayer setValue:@-1 forKeyPath:@"transform.scale.x"];

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多