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

Gow*_*i G 1 uinavigationcontroller uibarbuttonitem swift lottie

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

Lok*_* SN 6

只要利用 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)
Run Code Online (Sandbox Code Playgroud)

测试了这段代码,它对我来说很好用。