【发布时间】:2020-08-28 08:23:27
【问题描述】:
预期:点击心形图标时会出现绿点加载动画
结果:什么都没有发生,感觉就像冻结了,因为心脏动画也不起作用
错误:[未知进程名称] CGAffineTransformInvert:奇异矩阵。
Screenshot of the app(not working state)
代码:
struct ContentView: View {
@State private var count=0
@State var isPressed = false
@State var pressed = false
@State var isLoading = false
var body: some View {
VStack {
ZStack{
Circle()
.fill( isPressed ? Color(.systemGray4) : .red )
.overlay(Image(systemName:"heart.fill")
.foregroundColor(isPressed ? .red :.white)
.font(.system(size:100))
.scaleEffect( pressed ? 1.5 : 1.0)
)
.frame(width: 300, height: 300)
}
.onTapGesture{
self.isLoading.toggle()
withAnimation(.spring(response: 1,dampingFraction: 0.4, blendDuration :0.9)){
self.isPressed.toggle()
self.pressed.toggle()
self.isLoading.toggle()
}
}
HStack{
ForEach(0...4, id: \.self){index in
Circle()
.frame(width:10,height:10)
.foregroundColor(.green)
.scaleEffect(self.isLoading ? 1:0)
.animation(Animation.linear(duration:0.6).repeatForever().delay(0.2*Double(index)))
}
}
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
注意:当我不切换onTapGesture()块内的isLoading状态变量并在视图出现在onAppear()块内时直接将其设置为true时,加载动画有效
【问题讨论】: