Apply 3D rotation to iPhone view

2020-06-17 04:24发布

问题:

I would like apply a 3D rotation on a view (in particular to a UILabel) in iPhone. What's the simplest way to do this?

A code example will be much appreciated.

回答1:

For 2D rotation use:

//rotate label in 45 degrees
label.transform = CGAffineTransformMakeRotation( M_PI/4 );

For 3D transformations see this thread:

CATransform3D _3Dt = CATransform3DMakeRotation(radians(90.0f), 1.0, 0.0, 0.0);


回答2:

// 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];


回答3:

Swft 3 example

let rotationTransform = CATransform3DRotate(myView.layer.transform, CGFloat.pi, 1, 0, 0)

UIView.animate(withDuration: 0.5) {
    self.myView.layer.transform = rotationTransform
}