我一直在學習路徑形狀等等,并嘗試通過制作一個有很多影片可能性的矩形來練習。我取得的結果非常出乎意料(對我來說)。我在每個影片中使用的漸變都很奇怪
我試過查看是否有任何值意外更改但無濟于事。繼承人有這個問題的代碼:
import SwiftUI
struct ColorCyclingCircle: View, Animatable {
var amount = 0.0
var steps = 100.0
var saturation = 1.0
var brightness1 = 1.0
var brightness2 = 0.5
var animatableData: AnimatablePair<AnimatablePair<Double, Double>, Double> {
get { AnimatablePair(AnimatablePair(steps, amount), saturation) }
set {
steps = newValue.first.first
amount = newValue.first.first
saturation = newValue.second
}
}
var body: some View {
ZStack {
ForEach(0..<Int(steps), id: \.self) { value in
Rectangle()
.inset(by: Double(value))
.strokeBorder(
LinearGradient(
gradient: Gradient(colors: [
color(for: value, brightness: brightness1),
color(for: value, brightness: brightness2)
]),
startPoint: .top,
endPoint: .bottom
),
lineWidth: 2
)
}
}
.drawingGroup()
}
func color(for value: Int, brightness: Double) -> Color {
var targetHue = Double(value) / Double(steps) amount
if targetHue > 1 {
targetHue -= 1
}
return Color(hue: targetHue, saturation: saturation, brightness: brightness)
}
}
struct ContentView: View {
@State private var colorCycle = 0.0
@State private var steps = 100.0
@State private var saturation = 1.0
@State private var brightness1 = 1.0
@State private var brightness2 = 0.5
var body: some View {
VStack {
ColorCyclingCircle(amount: colorCycle, steps: steps, saturation: saturation, brightness1: brightness1, brightness2: brightness2)
.frame(width: 300, height: 300)
.onTapGesture {
withAnimation() {
saturation = saturation == 1 ? 0 : 1
colorCycle = colorCycle == 1 ? 0 : 1
saturation = saturation == 1 ? 0 : 1
}
}
Spacer()
.frame(height: 100)
Text("Color")
Slider(value: $colorCycle)
Text("Saturation")
Slider(value: $saturation)
Text("Brightness")
Slider(value: $brightness1)
Slider(value: $brightness2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
uj5u.com熱心網友回復:
在struct ColorCyclingCircle中,您得到的值是錯誤的amount:
改變:
amount = newValue.first.first
至:
amount = newValue.first.second
此外,在這段代碼中:
.onTapGesture {
withAnimation() {
saturation = saturation == 1 ? 0 : 1
colorCycle = colorCycle == 1 ? 0 : 1
saturation = saturation == 1 ? 0 : 1
}
}
你要換saturation兩次。在設定這些值之前,影片不會發生,所以最終結果是如果它saturation開始0時不是1. 那肯定不是你想要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/525799.html
標籤:迅速代码动画迅捷
下一篇:將xCode更新到v14.0.1后出現此錯誤-Swift編譯器錯誤:無法使用“@available”將存盤的屬性標記為可能不可用
