【问题标题】:How to rotate UILabel as a whole如何整体旋转UILabel
【发布时间】: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 轴不旋转。第二次旋转使标签看起来很窄。如何模拟透视?

【问题讨论】:

    标签: swift xcode


    【解决方案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 设置旋转之前。编辑了我的答案。
    • 我想知道为什么旋转顺序如此重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多