这是我的观点:
struct HomeView: View {
@ObservedObject var instance = Instance()
@State var dotColor = Color.gray
var repeatingAnimation: Animation {
Animation
.easeInOut(duration: 1.0)
.repeatForever()
}
var body: some View {
ZStack {
TabView(selection: $selectedTab) {
Settings(auth: auth)
.tabItem {
Image(systemName: "gear")
Text("Settings")
}.tag(0)
ZStack {
MapView(locationManager: locationManager)
Color.black.opacity(self.instance.status.opacity)
VStack {
Spacer()
Button(action: {
print("clicked")
withAnimation(self.repeatingAnimation) {
self.dotColor = Color.white
}
self.instance.changeStatus()
}){
HStack {
Text(self.instance.status.text)
.font(.system(size: 24))
.fontWeight(.bold)
.foregroundColor(Color.white)
Circle().frame(width: 12, height: 12)
.foregroundColor(self.dotColor)
.colorMultiply(self.dotColor)
.overlay(
Circle()
.stroke(Color.black, lineWidth: 1)
)
}
}.padding()
我的动画每秒钟将Circle()
的颜色从gray
更改为white
。但是,它也每秒更改我的ZStack
颜色不透明度(Color.black.opacity(self.instance.status.opacity)
)-即使它与我的动画无关。如果删除动画,则ZStack不透明度不会发生这种奇怪的行为。
我怎样才能解决这个问题?
最佳答案
您可以显式禁用某些修改器的动画,例如
Color.black.opacity(self.instance.status.opacity)
.animation(nil)