斯威夫特 5.x iOS 15
只是玩弄一個想法并遇到障礙?為什么我不能在這段代碼中改變這個狀態變數的值?
struct ContentView: View {
let timer = Timer.publish(every: 0.25, on: .main, in: .common).autoconnect()
@State var rexShape = CGRect(x: 0, y: 0, width: 128, height: 128)
var body: some View {
Arc(startAngle: .degrees(0), endAngle: .degrees(180), clockwise: true)
.stroke(Color.red, lineWidth: 2)
.frame(width: 128, height: 128, alignment: .center)
.onReceive(timer) { _ in
rexShape.height -= 10
}
}
}
struct Arc: Shape {
var startAngle: Angle
var endAngle: Angle
var clockwise: Bool
func path(in rect: CGRect) -> Path {
var path = Path()
path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.width / 2, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise)
return path
}
}
rexShape.height 行告訴我高度是一個只能獲取的屬性?這對我來說沒有意義,因為 rexShape 應該是一個變數?難道我只是失去理智了……
uj5u.com熱心網友回復:
如果您密切注意錯誤訊息,編譯器不會抱怨rexShape不可變,而是抱怨不可CGRect.height變。
要更改a的唯一heightor ,您需要通過它的屬性來完成。weightCGRectsize
rexShape.size.height -= 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435626.html
