我正在尝试创建一个动画,里面有一个小小的心形图标在抽动。我有两个图像,我相信足以创建效果,但我不知道如何创建这个动画效果。我尝试了几种方法,但似乎都不起作用。
您所能提供的任何帮助都将不胜感激。
这是我到目前为止所知道的:
@State var show : Bool = false
var body: some View {
VStack(alignment: .trailing){
ZStack{
BlackView()
if(show){
Image("heartOrgan1")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height:50)
.hidden()
Image("heartOrgan")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
} else {
Image("heartOrgan1")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
Image("heartOrgan")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
.hidden()
}
}
.onAppear(){
withAnimation { self.show.toggle()}
}
}
}
一般的想法是在代表心脏跳动的两个心脏图像之间循环切换。我对使用这些特殊的心脏图像很感兴趣,因为它们看起来像真正的心脏,我喜欢这样。
发布于 2021-01-08 16:03:11
发布于 2021-06-08 09:29:00
为此,您不一定需要两个图像。您可以使用一个图像并对其应用缩放效果。使一个图像放大,并增加一个延迟。然后让它重复。
示例:
@State private var animationAmount: CGFloat = 1
var body: some View {
ZStack {
Color.black
Image(systemName: "heart.fill")
.resizable()
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.red)
.scaleEffect(animationAmount)
.animation(.linear(duration: 0.1).delay(0.4).repeatForever(autoreverses: true))
.onAppear {
animationAmount = 1.2
}
}
}
https://stackoverflow.com/questions/65623840
复制相似问题