我正在使用SwiftUI,但我发现this Apple tutorial中的代码无法正常工作。我想将动画应用于scaleEffect
,而不是rotationEffect
,但是按照上述教程中的说明进行操作是行不通的-添加.animation(nil)
时,根本没有动画。这是错误还是我做错了什么?
struct ContentView: View {
@State private var showDetail = false
var body: some View {
Button(action: {
self.showDetail.toggle()
}) {
Image(systemName: "chevron.right.circle")
.imageScale(.large)
.rotationEffect(.degrees(showDetail ? 90 : 0))
.animation(nil)
.scaleEffect(showDetail ? 1.5 : 1)
.padding()
.animation(.easeInOut(duration: 2))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
答案 0 :(得分:1)
只需将.animation
放在.rotationEffect
之前,它就不会应用于效果
Image(systemName: "chevron.right.circle")
.imageScale(.large)
.scaleEffect(showDetail ? 1.5 : 1)
.padding()
.animation(.easeInOut(duration: 2))
.rotationEffect(.degrees(showDetail ? 90 : 0))