我正在try 创建一个基于另一个视图的状态有条件地设置动画(永远重复)的视图.
下面的按钮可以在一个视图中完成所有操作...但这个州都是internal.我希望是external.
struct ButtonBouncerView: View {
@State var active: Bool = false
var body: some View {
Circle()
.scaleEffect(active ? 1.08: 1)
.animation(Animation.default.repeatForever(autoreverses: true), value: active)
.frame(width: 100, height: 100)
.onTapGesture {
useTransaction()
}
.onAppear {
active = true
}
}
func useTransaction() {
var transaction = Transaction()
transaction.disablesAnimations = active ? true : false
withTransaction(transaction) {
withAnimation {
active.toggle()
}
}
}
}
我试着这样做,使用事务API.然而,当我敲击时,这只会产生奇怪的动画效果,基本上是某种奇怪的摇摆,比如运动.
struct OutsideControllableButtonView: View {
@State private var active: Bool = false
var body: some View {
VStack {
OutsideControllableButtonScaler(active: $active)
Button(action: { // CONTROLLER BUTTON!
active.toggle()
}) {
Text("Control Button")
}
.padding()
.background(Color.green)
.foregroundColor(.white)
}
}
}
struct OutsideControllableButtonScaler: View {
// When Active, the button
@Binding var active: Bool
var body: some View {
Circle()
.scaleEffect(active ? 1.08: 1)
.animation(Animation.default.repeatForever(autoreverses: true), value: active)
.frame(width: 100, height: 100)
.onChange(of: active) { newValue in
var transaction = Transaction(animation: newValue ? Animation.default.repeatForever(autoreverses: true) : nil)
transaction.disablesAnimations = true
withTransaction(transaction) {
self.active = newValue
}
}
}
}
你有什么办法解决这个问题吗?