Swiftui 迅捷+;乐蒂iOS-播放动画

Swiftui 迅捷+;乐蒂iOS-播放动画,swiftui,lottie,lottie-ios,Swiftui,Lottie,Lottie Ios,我以蒙托为例,介绍了如何在SwiftUI中播放乐透动画。()然而,我想知道是否有人能帮助我理解如何最初呈现动画的第一帧,但只有在用户以按钮格式点击动画后才能播放动画 我当前的LottieButton文件如下: /*更新-下面的代码现在可以在点击时播放动画*/ import SwiftUI import Lottie struct LottieButton: UIViewRepresentable { /// Create a button. let animationButton = Anim

我以蒙托为例,介绍了如何在SwiftUI中播放乐透动画。()然而,我想知道是否有人能帮助我理解如何最初呈现动画的第一帧,但只有在用户以按钮格式点击动画后才能播放动画

我当前的LottieButton文件如下:

/*更新-下面的代码现在可以在点击时播放动画*/

import SwiftUI
import Lottie

struct LottieButton: UIViewRepresentable {
/// Create a button.
let animationButton = AnimatedButton()
var filename = "LottieLogo2"

func makeUIView(context: UIViewRepresentableContext<LottieButton>) -> UIView {
    let view = UIView()

    let animation = Animation.named(filename)
    animationButton.animation = animation
    animationButton.contentMode = .scaleAspectFit

    animationButton.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(animationButton)

    animationButton.clipsToBounds = false
       /// Set animation play ranges for touch states
    animationButton.setPlayRange(fromMarker: "touchDownStart", toMarker: "touchDownEnd", event: .touchUpInside)
       animationButton.setPlayRange(fromMarker: "touchDownEnd", toMarker: "touchUpCancel", event: .touchUpOutside)
       animationButton.setPlayRange(fromMarker: "touchDownEnd", toMarker: "touchUpEnd", event: .touchUpInside)

    NSLayoutConstraint.activate([
        animationButton.heightAnchor.constraint(equalTo: view.heightAnchor),
        animationButton.widthAnchor.constraint(equalTo: view.widthAnchor),
    ])

    return view

}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieButton>) {

    }
}

如果只想在按下按钮时显示
lottie
文件,可以设置
@State
切换,并在切换变量时显示您的lottie

示例代码:


@状态变量toggleValue=false
var body:一些观点{
VStack{
按钮(操作:{
self.toggleValue.toggle()
}) {
VStack{
如果切换值{
LottieView(文件名:“收件箱通知”)
.框架(宽度:100)
}
文本(“按钮”)
}
}
}
}

在深入阅读Lottie文档后,我看到有注释代码显示了如何实现动画按钮。我在LottieButton中编辑了上面的代码以设置按钮,然后我可以将其放置到视图中,并在点击时使其动画化。我在顶部编辑了代码以显示其工作原理。

嗯,我希望动画的初始帧最初显示,但我只希望它在点击时播放。因此,如果是TwitterHeart,我希望心脏显示,然后点击时,播放并显示填充+爆炸动画。使用LottieButton时,如何处理点击以运行函数
.ontapsignate
可以工作,但使用它动画无法工作。有什么例子吗?
struct InboxView: View {
var body: some View {
    VStack { 
        LottieButton(filename: "inbox-notification")
        .frame(width: 100)
            }   
    }
}