1

I have written a code which should turn UILabel by Z axis and then by Y axis to simulate perspective. Here is the sample code:

var t: CATransform3D = CATransform3DIdentity
t = CATransform3DRotate(t, 270 * CGFloat.pi / 180, 0, 0, 1) // rotate by z axis
t = CATransform3DRotate(t, -50 * CGFloat.pi / 180, 0, 1, 0) // rotate by y axis
t.m34 = 1.0 / -500
logLabel.transform3D = t // implement sequence of rotations to label
contentView.addSubview(logLabel)

It rotates by Z axis correctly, but it does not rotate by Y axis. The second rotation makes the label to look narrow. How to simulate perspective?

ios coder
  • 1
  • 4
  • 31
  • 91

1 Answers1

2

Edit: You must set the m34 before you set the rotation.

var transform = CATransform3DIdentity
transform.m34 = -1.0 / 500.0
transform = CATransform3DRotate(transform, 65 * CGFloat.pi / 180, 1, 0, 0)
        
label.layer.transform = transform

Result:

3d transformed label, star wars style


Old answer:

You might want to change the m34

t.m34 = 1.0 / -500

This sets how "far" the label is... more details here

aheze
  • 24,434
  • 8
  • 68
  • 125