首页 > 解决方案 > 调用 'rotationEffect(_:anchor:)' 的结果未使用

问题描述

这个想法很简单,我有一个按钮,一旦点击了我会旋转Text(titleContent)

点击这里查看视图

视图内部:

                ZStack {
                VStack {
                    Text(titleContent)
                        .fontWeight(.heavy)
                        .font(Font.custom("Benguiat Bold", size: 35))
                        .foregroundColor(.red)
                        .padding()
                    
                    Button(action: {
                        self.updateText()
                    }, label: {
                        Text("Enter upside down")
                            .foregroundColor(.white)
                            .font(.title2)
                            .fontWeight(.bold)
                            .padding()
                            .border(Color.green, width: 3)
                            .cornerRadius(10)
                            .padding()
                    })
                }
            }
        )
func updateText() {
    Text(titleContent)
        .fontWeight(.heavy)
        .font(Font.custom("Benguiat Bold", size: 35))
        .foregroundColor(.red)
        .rotationEffect(.degrees(180))
}

在函数内部,updateText()我收到以下警告:

调用 'rotationEffect(_:anchor:)' 的结果未使用

标签: iosswiftswiftui

解决方案


与其尝试View从作为Button操作调用的函数内部返回 aa new,不如修改一个@State属性,以在用户点击按钮时控制旋转角度。

struct RotatingView: View {
    @State private var rotationAngle: Double = 0
    let titleContent: String

    var body: some View {
        ZStack {
            VStack {
                Text(titleContent)
                    .fontWeight(.heavy)
                    .font(Font.custom("Benguiat Bold", size: 35))
                    .foregroundColor(.red)
                    .padding()
                    .rotationEffect(.degrees(rotationAngle))

                Button(action: {
                    rotationAngle += 180
                }, label: {
                    Text("Enter upside down")
                        .foregroundColor(.white)
                        .font(.title2)
                        .fontWeight(.bold)
                        .padding()
                        .border(Color.green, width: 3)
                        .cornerRadius(10)
                        .padding()
                })
            }
        }
    }
}

推荐阅读