3D Cube Problem! Part 1

2019-04-11 10:52发布

I have created a 3D cube in iphone using CALayer's. Now I wanted to rotate that cube(CALayer) at 90˚ when user double taps on it.

I was able to rotate that cube(CALayer) to 90˚ once but when I double tap the cube(CALayer) is not rotating.

Here is the code that I used to rotate the cube(CALayer)

CATransform3D x = CATransform3DRotate(currentLayer.sublayerTransform, M_PI / 2, 0, 0, 1);
currentLayer.transform = x;

Can anyone help in this. What I'm doing wrong.


PS. For the people who are wondering how I got the degree sign then here is the trick

Option + K

1条回答
Anthone
2楼-- · 2019-04-11 11:34

its because you are not changing the angle of rotation .... to understand this lets say you are passing M_PI/2 each time to that method .... so CATransform3DRotate do not rotate it to next 90˚ rather it rotate the layer to the specified angle in this case its 90... so you are not getting any chage because it already at 90˚ ..... so to get correct result do this

static float angle = M_PI / 2;//dont make it static rather make it a global variable
angle += M_PI / 2;
CATransform3D x = CATransform3DRotate(currentLayer.sublayerTransform,angle, 0, 0, 1);
currentLayer.transform = x;
查看更多
登录 后发表回答