Display Gifts | Play normal gift | Play full-screen gift |
| |
Gift system`s Structure diagram | Gift system`s Sequence diagram |
TUIGiftListView
: A gift panel that presents the gift list, sends gifts, and recharges.TUIGiftPlayView
: A panel that plays gifts and automatically listens to gift messages.TUIGiftListView
provides the setGiftList
interface, which can be used to set gift materials.let giftListView: TUIGiftListView = TUIGiftListView(groupId: xxx) //generator giftListView objectlet giftList: [TUIGift] = ... //you can change gift materials heregiftListView.setGiftList(giftList) //set gift materials of giftListPanleView
TUIGift
are as follows:giftId: String
: Gift IDgiftName: String
: Gift NameimageUrl: String
: Image displayed on the gift panelanimationUrl: String
: SVGA animation URLprice: Int
: Gift PriceextInfo: [String: AnyCodable]
: Custom extension informationanimationUrl
is empty, the gift playing effect will be an ordinary play, and the content played will be the image linked by the imageUrl. If the animationUrl
is not empty, the playing effect will be a full-screen play, and the content played will be the corresponding svga animation.onSendGift
delegate function in the TUIGiftListViewDelegate
of TUIGiftListView
to get the gift count and gift information. After preprocessing, you can call the sendGift
function of TUIGiftListView
for the actual sending of gifts.giftListView.delegate = selfextension ViewController:TUIGiftListViewDelegate
{
func onSendGift(giftListView view: TUIGiftListView, giftModel: TUIGift, giftCount: Int) {//...Here is the preprocessing, such as verifying the current user's balance and other operations.let receiver = TUIGiftUser()//...Set the gift recipient information here.view.sendGift(giftModel: giftModel, giftCount: giftCount, receiver: receiver)}}
TUIGiftPlayView
will receive and play gift messages by itself.let giftDisplayView: TUIGiftPlayView = TUIGiftPlayView(groupId:xxx)
TUIGiftPlayView
requires full-screen integration.giftPlayView:onPlayGift
delegate function in the TUIGiftPlayViewDelegate
of TUIGiftPlayView
.extension ViewController: TUIGiftPlayViewDelegate {func giftPlayView(_ giftPlayView: TUIGiftPlayView, onPlayGift gift: TUIGift, giftCount: Int, sender: TUIGiftUser, receiver: TUIGiftUser) {//...You can handle this on your own here.}}
TUIGiftListView
provides the setBalance
interface, which can be used to set the balance value displayed on the gift panel.giftListView.setBalance(xxx)
onRecharge
delegate function in the TUIGiftListViewDelegate
of TUIGiftListView
, which can be used to receive the click event of the recharge button thrown by the gift display panel, and connect to your own recharge system here.extension ViewController:TUIGiftListViewDelegate
{
func onRecharge(giftListView view: TUIGiftListView) {//...This can be used to connect to your own recharge system. After the recharge is completed,//you can call view.setBalance(xxx) to set the balance display of the gift display panel.}}
onSendGift
delegate function in the TUIGiftListViewDelegate
of TUIGiftListView
, connect to the customer's own business server, complete the balance verification, gift billing, and consumption statistics, and then call the sendGift
of TUIGiftListView
to send the gift message.giftListView.delegate = selfextension ViewController:TUIGiftListViewDelegate
{
func onSendGift(giftListView view: TUIGiftListView, giftModel: TUIGift, giftCount: Int) {//...Connect to the customer's own business server here to complete balance verification, gift billing, consumption statistics, etclet receiver = TUIGiftUser()//...Set the gift recipient information here.view.sendGift(giftModel: giftModel, giftCount: giftCount, receiver: receiver)}}
// Source code path:TUILiveKit/Source/LiveRoom/View/Audience/Component/AudienceLivingView.swift
private lazy var giftPanelView: TUIGiftListView = {let view = TUIGiftListView(groupId: liveRoomInfo.roomId.value)giftCloudServer.queryGiftInfoList { [weak self] error, giftList inguard let self = self else { return }DispatchQueue.main.async {if error == .noError {view.setGiftList(giftList)} else {self.makeToast("query gift list error, code = \\(error)")}}}return view}()
giftCloudServer.queryGiftInfoList
on their own, get a custom gift list [TUIGift]
, and set the gift list through view.setGiftList
.// Source code path:TUILiveKit/Source/LiveRoom/View/Audience/Component/AudienceLivingView.swift
private lazy var giftPanelView: TUIGiftListView = {let view = TUIGiftListView(groupId: liveRoomInfo.roomId.value)giftCloudServer.queryBalance { [weak self] error, balance inguard let self = self else { return }DispatchQueue.main.async {if error == .noError {view.setBalance(balance)} else {self.makeToast("query balance error, code = \\(error)")}}}return view}()
giftCloudServer.queryBalance
on their own, obtain the gift balance, and update the gift balance through view.setBalance
.// Source code path:TUILiveKit/Source/LiveRoom/View/Audience/Component/AudienceLivingView.swift
func onSendGift(giftListView view: TUIGiftListView, giftModel: TUIGift, giftCount: Int) {let receiver = TUIGiftUser()receiver.userId = liveRoomInfo.anchorInfo.value.userIdreceiver.userName = liveRoomInfo.anchorInfo.value.name.valuereceiver.avatarUrl = liveRoomInfo.anchorInfo.value.avatarUrl.valuereceiver.level = "0"giftCloudServer.sendGift(sender: TUILogin.getUserID() ?? "",receiver: receiver.userId,giftModel: giftModel,giftCount: giftCount) { [weak self] error, balance inguard let self = self else { return }if error == .noError {view.sendGift(giftModel: giftModel, giftCount: giftCount, receiver: receiver)view.setBalance(balance)} else {self.makeToast(.balanceInsufficientText)}}}
giftCloudServer.sendGift
on their own. The main logic is to first connect to the customer's own business server to verify the balance, and after the verification is passed, the server will charge and count the consumption records, and finally call back the result to the client. After receiving the successful callback, the client sends the gift message through the sendGift
of the GiftListView
, and then updates the gift balance through setBalance
.// Source code path://TUILiveKit/Source/LiveRoom/View/Audience/Component/AudienceLivingView.swift
//TUILiveKit/Source/LiveRoom/View/Anchor/Living/AudienceLivingView.swift
func giftPlayView(_ giftPlayView: TUIGiftPlayView, onPlayGiftAnimation gift: TUIGift) {giftCacheService.request(urlString: gift.animationUrl) { error, data inguard let data = data else { return }if error == 0 {DispatchQueue.main.async {giftPlayView.playGiftAnimation(animationData: data)}}}}
giftCacheService.request
on their own. Lexter, loads the animation to get the data (Data
type), and then plays the gift animation through playGiftAnimation
of TUIGiftPlayView
.
Was this page helpful?