如何快速将贴纸视图添加到浏览器视图? [英] How to add sticker VIEWS to browser view in swift?

查看:35
本文介绍了如何快速将贴纸视图添加到浏览器视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Alright, like everyone Im new to ms stickers in Swift but I am trying to figure out the purpose of / difference between an mssticker and msstickerview. I have read the API here https://developer.apple.com/reference/messages/msstickerview/1648434-sticker but cant find an answer to this relatively simple problem - it seems you can only add MSStickers (not StickerViews) to an MSStickerBrowserView, which is the only way to display them. I however need to add StickerVIEWS because I have a custom sticker view class I am trying to implement.

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

My stickers are added to my browser view here:

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 数组中.

As you can see, I am creating both a sticker and a sticker view for each sticker - my stickers are stored in the stickers array and sticker views in stickerViews array.

以下是浏览器的填充方式:

Here is how the browser is populated:

 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
    }

我尝试将这些方法的返回类型更改为 StickerView 并改为返回stickerView 数组

I have tried changing the return type on these methods to StickerView and returning the stickerView array instead

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

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

however this gets me the following error:

messagesviewcontroller 不符合协议msstickerbrowserviewdatasource

messagesviewcontroller does not conform to protocol msstickerbrowserviewdatasource

因为所需的功能没有像以前那样实现.一个显示贴纸如何查看?我做错了什么?

Because the required function isn't being implemented as it was before. How does one display sticker views? What am I doing wrong?

推荐答案

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

You cannot add MSStickerViews to MSStickerBrowserView. In order to use your subclass, you will have to build your own interface per Apple's documentation for MSStickerBrowserView:

如果您需要额外的自定义,您必须使用 MSStickerView 对象构建您自己的用户界面.

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

如果您想模拟浏览器视图的外观,只需使用 UICollectionView 并使用贴纸视图填充单元格

If you want to emulate the look of the browser view, you can just use a UICollectionView and populate the cells with your sticker views

这篇关于如何快速将贴纸视图添加到浏览器视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆