ios 如何在SwiftUI中旋转文本,按钮,矩形?

wxclj1h5  于 2023-05-19  发布在  iOS
关注(0)|答案(2)|浏览(227)

如何使用SwiftUI将TextButton和SwiftUI的其他设计控件旋转90度?
我有意将我的问题建模为这个问题的SwiftUI版本:How can you rotate text for UIButton and UILabel in Swift?

当前输出:

预期输出:

pwuypxnk

pwuypxnk1#

使用任何一个.rotationEffect()方法顺时针旋转任何View,包括ButtonText
例如,这会使Text绕其原点(其框架的中心)旋转:

Text("Turtle Rock")
.rotationEffect(Angle(degrees: 90)))

使用带锚参数的重载方法绕不同的点旋转。
例如,这将围绕其帧的左下角点旋转Text

Text("Turtle Rock")
.rotationEffect(Angle(degrees: 90), anchor: .bottomLeading)

也可以使用弧度进行旋转:

Text("Turtle Rock")
.rotationEffect(radians: Double.pi / 2)
8wigbo56

8wigbo562#

要做任何类型的(复杂的)旋转,使用3D作为.rotation3DEffect(.degrees(45), axis: (x: 0.5, y: 0, z: 0))(这将是星球大战学分效果)。这两个应该是等价的:Text("Turtle Rock").rotationEffect(Angle(degrees: 90)))Text("Turtle Rock").rotation3DEffect(.degrees(90), axis: (x: 0, y: 0, z: 1))

相关问题