使用此代码在我的应用程序中创建一个Lottie动画。 某种程度上,我在UI中看不到任何结果。您是否知道错误在哪里?
导入基金会 导入UIKit 进口乐透
TestAnimation2类:UIViewController {
var menuOn = false
var menuButton: LOTAnimationView?
var itemButtonFrame = CGRect(x: 0, y: 70, width: 100, height: 100)
override func viewDidLoad() {
super.viewDidLoad()
}
//um es interaktiv / touchable zu machen
func addToggleRecognizer(_ animationView: LOTAnimationView) {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleMenu(recognizer:)))
animationView.addGestureRecognizer(tapRecognizer)
}
func addMenuButton(_ on: Bool) {
//if exists "reset" the state to nil
if menuButton != nil {
self.menuButton?.removeFromSuperview()
self.menuButton = nil
}
let animation = menuOn ? "checkon" : "checkoff"
menuButton = LOTAnimationView(name: animation)
guard let menuButton = menuButton else {
return
}
menuButton.isUserInteractionEnabled = true
menuButton.frame = itemButtonFrame
menuButton.contentMode = .scaleAspectFill
addToggleRecognizer(menuButton)
view.addSubview(menuButton)
}
@objc func toggleMenu(recognizer: UITapGestureRecognizer) {
menuOn ? setMenuOffAndPerformAction() : setMenuOnAndPerformAction()
}
func setMenuOnAndPerformAction() {
menuButton?.play(completion: { (success) in
self.menuOn = true
self.addMenuButton(self.menuOn)
DispatchQueue.main.async {
print("menu is ON")
//update UI on main thread
}
})
}
func setMenuOffAndPerformAction() {
menuButton?.play(completion: { (success) in
self.menuOn = false
self.addMenuButton(self.menuOn)
DispatchQueue.main.async {
print("menu is OFF")
//update UI on main thread
}
})
}
}
预先感谢!