How to add sticker VIEWS to browser view in swift?
Asked Answered
C

1

0

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?

Chrotoem answered 29/9, 2016 at 1:32 Comment(0)
S
0

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

Sannyasi answered 29/9, 2016 at 5:30 Comment(2)
Ok, thank you- how can I build a uicollectionview? Do you have any experience with doing that? Just new to the conceptChrotoem
it's very similar to table views - plenty of examples out there: raywenderlich.com/136159/…Sannyasi

© 2022 - 2024 — McMap. All rights reserved.