我想在iPhone上对一个视图(尤其是UILabel)应用3D旋转。如何最简单地实现这个功能?
提供代码示例将非常感激。
对于2D旋转,请使用以下方法:
//rotate label in 45 degrees
label.transform = CGAffineTransformMakeRotation( M_PI/4 );
有关3D变换,请参见该帖子:
CATransform3D _3Dt = CATransform3DMakeRotation(radians(90.0f), 1.0, 0.0, 0.0);
// flipping view along axis
// this will rotate view in 3D along any axis
[UIView beginAnimations:nil context:nil];
CATransform3D _3Dt = CATransform3DRotate(self.layer.transform, 3.14, 1.0, 0.0,0.0);
[UIView setAnimationRepeatCount:100];
[UIView setAnimationDuration:0.08];
self.layer.transform=_3Dt;
[UIView commitAnimations];
Swift 3 示例
let rotationTransform = CATransform3DRotate(myView.layer.transform, CGFloat.pi, 1, 0, 0)
UIView.animate(withDuration: 0.5) {
self.myView.layer.transform = rotationTransform
}