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

问题描述 投票:0回答:1

正在尝试在UIbarbuttonItem中设置抽奖动画。是否可以在UIBarButtonItem

swift uinavigationcontroller uibarbuttonitem lottie
1个回答
0
投票

只需使用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(closeButtonTapped(_:))))
//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)

测试了此代码,对我来说很好用。

© www.soinside.com 2019 - 2024. All rights reserved.