如何在 swift 中将贴纸视图添加到浏览器视图?

How to add sticker VIEWS to browser view in swift?

好吧,就像 Swift 中的每个人一样,我是 ms 贴纸的新手,但我想弄清楚 mssticker 和 msstickerview 之间的目的/区别。我已阅读 API 此处 https://developer.apple.com/reference/messages/msstickerview/1648434-sticker 但无法找到这个相对简单问题的答案 - 似乎您只能将 MSStickers(不是 StickerViews)添加到 MSStickerBrowserView,这是显示它们的唯一方法。但是我需要添加 StickerVIEWS,因为我有一个自定义贴纸视图 class 我正在尝试实现。

我的贴纸已添加到我的浏览器视图中:

func loadStickers() {


        var url: URL?
        var i = 1
        while true {
            url = Bundle.main.url(forResource: "test\(i)", withExtension: "png") //change test for packs
            print("URL IS THIS: \(url)")
            guard let url = url else { break }

            //make it a sticker
            let sticker = try! MSSticker(contentsOfFileURL: url, localizedDescription: "")
            let stickerView = InstrumentedStickerView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            stickerView.sticker = sticker
            stickerView.delegate = self

            stickerViews.append(stickerView)
            stickers.append(sticker)

            i += 1
        }

    }


func createStickerBrowser() {
        let controller = MSStickerBrowserViewController(stickerSize: .regular)

        addChildViewController(controller)
        view.addSubview(controller.view)

        controller.stickerBrowserView.backgroundColor = UIColor.white
        controller.stickerBrowserView.dataSource = self

        //resize this programmatically later
        view.topAnchor.constraint(equalTo: controller.view.topAnchor).isActive = true
        view.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor).isActive = true
        view.leftAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true
        view.rightAnchor.constraint(equalTo: controller.view.rightAnchor).isActive = true
    }

如您所见,我正在为每个贴纸创建贴纸和贴纸视图 - 我的贴纸存储在 stickers 数组中,贴纸视图存储在 stickerViews 数组中。

浏览器的填充方式如下:

 func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
        return stickers.count
    }

    func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
        return stickers[index] //this isnt displaying stickerveiws only stickers
    }

我尝试将这些方法的 return 类型更改为 StickerView 并 return 改为使用 stickerView 数组

 func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSStickerView {
        return stickerViews[index] //this isnt displaying stickerveiws only stickers
    }

但是这让我出现以下错误:

messagesviewcontroller does not conform to protocol msstickerbrowserviewdatasource

因为没有像以前那样实现所需的功能。如何显示贴纸视图?我做错了什么?

您不能将 MSStickerViews 添加到 MSStickerBrowserView。为了使用您的子类,您必须根据 Apple 的 MSStickerBrowserView:

文档构建自己的界面

If you need additional customizations, you must build your own user interface using MSStickerView objects.

如果你想模拟浏览器视图的外观,你可以只使用 UICollectionView 并用你的标签视图填充单元格