我有一个加号,是我用线条画的。现在我想旋转这些线条,使其看起来像一个 X 符号。我尝试旋转线条,但没有办法。
self.shapeLayer1.transform = CATransform3DMakeRotation( (CGFloat) (GLKMathDegreesToRadians(-45)),0, 0, 0)
self.shapeLayer2.transform = CATransform3DMakeRotation( (CGFloat) (GLKMathDegreesToRadians(-45)), 0, 0, 0)
你们可以看到,我在 x、y、z 位置放了零。!!我尝试了不同的值。但是没有得到实际的旋转。如果有人有任何想法,请与我分享。有时线条会移动到另一个点并旋转。
最佳答案
看起来 x、y、z 参数定义了旋转轴。由于我们要绕 xy 旋转,因此您的旋转轴应该是 z 轴,即 0,0,1。
self.shapeLayer1.transform = CATransform3DMakeRotation( (CGFloat) (GLKMathDegreesToRadians(45)),0, 0, 1)
self.shapeLayer2.transform = CATransform3DMakeRotation( (CGFloat) (GLKMathDegreesToRadians(-45)), 0, 0, 1)
关于绕线的非中心点旋转时遇到的问题,如果您无法重画以 0,0,0 为中心的线,您还可以使用以下代码将其转换为 0 ,0,0,旋转,然后将其变换回您需要的位置:
CGFloat tx = 1.0,ty = 2.0,tz = 0; // Modify these to the values you need
CATransform3D t = CATransform3DMakeTranslation (tx, ty, tz);
t = CATransform3DRotate(t,(CGFloat) (GLKMathDegreesToRadians(45)),0, 0, 1);
self.shapeLayer1.transform = CATransform3DTranslate(t,-tx,-ty,-tz);
CATransform3D t = CATransform3DMakeTranslation (tx, ty, tz);
t = CATransform3DRotate(t,(CGFloat) (GLKMathDegreesToRadians(-45)),0, 0, 1);
self.shapeLayer2.transform = CATransform3DTranslate(t,-tx,-ty,-tz);
关于ios - 在ios中旋转一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25112582/