How to add sticker VIEWS to browser view in swift?

旧巷老猫 提交于 2019-12-25 07:59:44

问题


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
    }

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
    }

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 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?


回答1:


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:

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

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



来源:https://stackoverflow.com/questions/39760009/how-to-add-sticker-views-to-browser-view-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!