swift - 有没有办法在 SwiftUI 中向 Lottie 动画开关添加动作
问题描述
有没有办法为乐蒂动画开关访问“onTapGesture”(或类似的)。我想在开关打开时更改 CoreData 项的属性 (isComplete)。下面是我的 Lottie 按钮视图。非常感谢。
import SwiftUI
import Lottie
struct LottieButton: UIViewRepresentable {
@State var task: Task
// Create a switch
let animationSwitch = AnimatedSwitch()
var filename = "checkmark"
func makeUIView(context: UIViewRepresentableContext<LottieButton>) -> UIView {
let view = UIView()
let animation = Animation.named(filename)
animationSwitch.animation = animation
animationSwitch.contentMode = .scaleAspectFit
animationSwitch.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationSwitch)
animationSwitch.clipsToBounds = false
animationSwitch.setProgressForState(fromProgress: 0, toProgress: 0.4, forOnState: true)
// fail attempt #1
// self.onTapGesture {
// self.task.isComplete.toggle()
// print(self.task.isComplete)
// }
// fail attempt #2
// if animationSwitch.isOn{
// task.isComplete.toggle()
// print(task.isComplete)
// action()
// }
NSLayoutConstraint.activate([
animationSwitch.heightAnchor.constraint(equalTo: view.heightAnchor),
animationSwitch.widthAnchor.constraint(equalTo: view.widthAnchor),
])
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieButton>) {
}
}
解决方案
推荐阅读
- google-analytics - Google Analytics:如何查看访问我网站的用户的 IP 地址?
- angular - Angular如何添加一个数字大于以前的按钮?
- python - 使用 ImageDataGenerator 进行数据增强
- entity-framework - 使用 Single 和 SingleOrDefaultAsync 时的延迟加载
- javascript - 使用 replace() 时如何修复“未终止的正则表达式文字”错误
- android - 定义使用 gluon mobile 编译 Android 应用程序的原型
- java - Java trait 解决方法
- java - 模拟类在单元测试执行时显示为空
- css - 溢出:由行高引起的自动(错误?)
- javascript - DataView.getFloat32() 返回 NaN