【发布时间】:2020-07-07 18:08:24
【问题描述】:
我有一个简单的项目,它在点击按钮时运行 Lottie 动画。
struct ContentView: View {
@State var number = 0
@State var toogleValue : Bool = false
var body: some View {
ZStack {
Color.green.opacity(0.2)
ZStack {
LottieButton(filename: "17247-happy-flower")
.frame(width: 200)
.onTapGesture {
self.number += 1
}
Text("number = \(self.number)")
.offset( y: 150)
}
}.edgesIgnoringSafeArea(.all)
}
}
我想要实现的是:每当用户点击按钮时,动画应该开始并且数字计数器应该增加 1。
现在发生了什么:计数器加 1,但动画没有播放。
我尝试了什么:当我评论 // onTapGesture {self.number += 1} 时,动画工作正常,但计数器不工作。
这是 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>) {
}
}
我认为问题是由于视图的层次结构造成的,但我不确定。
【问题讨论】:
标签: swift animation swiftui lottie