如何整体旋转UILabel

Posted

技术标签:

【中文标题】如何整体旋转UILabel【英文标题】:How to rotate UILabel as a whole 【发布时间】:2020-09-28 17:14:10 【问题描述】:

我写了一个代码,它应该通过 Z 轴转动 UILabel,然后通过 Y 轴来模拟透视。下面是示例代码:

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)

Z 轴正确旋转,Y 轴不旋转。第二次旋转使标签看起来很窄。如何模拟透视?

【问题讨论】:

【参考方案1】:

编辑:您必须在设置旋转之前设置m34

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

结果:


旧答案:

您可能想更改m34

t.m34 = 1.0 / -500

这设置标签的“距离”...更多详情here

【讨论】:

我添加了代码,但不是预期的效果。看起来 UILabel 是 2d 的,我尝试在 3d 空间上模拟它。它更窄,但我希望文本的开头更小,结尾更大,即梯形而不是矩形:) 啊,好的。你找到解决方案了吗?抱歉回复晚了。 刚刚意识到您需要设置m34 设置旋转之前。编辑了我的答案。 我想知道为什么旋转顺序如此重要。

以上是关于如何整体旋转UILabel的主要内容,如果未能解决你的问题,请参考以下文章

CAD中如何旋转图形?

CAD的三维立体图形如何旋转角度?

web app 页面旋转整体样式问题

cad如何移动图形和旋转图形?求具体的方法

LeetCode 81.搜索旋转排序数组 II

35three.js鼠标控制物体旋转缩放