2012-04-27 85 views
2

我有一个需要转换的图层。目前我使用如下:CATransform3DRotate水平翻转

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

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

+0

是否可以详细介绍目前的结果你的手所期望的结果? – sch 2012-04-27 22:17:28

+0

它只是需要水平翻转,不知道如何解释它! – 2012-04-27 22:35:38

回答

9

你需要:

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

的刻度与-1是一个翻转。想象一下,你正在水平压缩图像,并且你超过零。

+0

它给了我以下错误:将'int'传递给不兼容类型'CATransform3D'的参数并发出警告:函数CATransform3DMakeRotate的隐式声明在C99中无效。 – 2012-04-27 22:34:16

+1

@NicHubbard - 它应该是'CATransform3DMakeRotation'而不是'CATransform3DMakeRotate'。 +1 – sch 2012-04-27 22:38:23

+0

嗯,好吧,现在它倒过来了...... – 2012-04-27 22:40:50

4

我个人觉得使用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"]; 
3

由于参数这里是

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);