【发布时间】:2021-03-25 10:59:34
【问题描述】:
我有一个图像,我希望它在 y 轴上旋转,并在我按下按钮时向视图的底部移动。我尝试使用.onChange,但我收到错误“调用'动画'的结果未使用”,我理解其中的含义,但我既不明白为什么会出现,也不明白如何修复它。
这是我的代码:
import SwiftUI
struct ContentView : View {
@State var animateCoin = false
@Environment(\.colorScheme) var colorScheme
var body: some View {
Rectangle()
.foregroundColor(colorScheme == .dark ? .white : .black)
.frame(width: 150, height: 150, alignment: .center)
.offset(y: -60)
.mask(Image("Coin") //That's the name of the Image I have in the assets
.resizable()
.onChange(of: animateCoin, perform: { value in
self.animation(.easeIn) //I put .easeIn just as an example, because the .rotation3DEffect gives me a red warning
}))
Button(action: { animateCoin = true } ) {
ZStack {
RoundedRectangle(cornerRadius: 10)
.foregroundColor(.green)
.frame(width: 100, height: 40, alignment: .center)
.shadow(radius: 10)
Text("Animate")
.foregroundColor(.white)
.shadow(radius: 20)
}
}
}}
图像被设置为蒙版,以便我可以根据亮或暗模式轻松控制其颜色。
感谢所有帮助我的人!
【问题讨论】:
标签: image animation button swiftui rotation