将 Lottie 动画设置为导航栏中的 UIBarbuttonitem swift

Set Lottie animation to UIBarbuttonitem in navigation bar swift

我正在尝试在导航控制器的导航栏按钮中设置 lottie 动画,但我不知道如何将动画设置为 UIbarButton

就用UIBarButtonItem(customView: yourView)

//Initialise a Lottie view with frame
let customAnimationView = AnimationView(name: "yourLottieFileName")
customAnimationView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

//Do your configurations
customAnimationView.loopMode = .loop
customAnimationView.backgroundBehavior = .pauseAndRestore

//Add a tap gesture
customAnimationView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(yourViewTapped(_:)))) //Declare an @objc function to handle your view's tap 
//And play
customAnimationView.play()

//Initilise a bar button with this custom Lottie view and use it
yourBarButton = UIBarButtonItem(customView: customAnimationView)
navigationItem.setLeftBarButton(yourBarButton, animated: true)

测试了这段代码,它对我来说工作正常。