好吧,就像每个人一样,我是 Swift 中的 ms 贴纸的新手,但我试图弄清楚 mssticker 和 msstickerview 的目的/区别。我已经阅读了这里的 API https://developer.apple.com/reference/messages/msstickerview/1648434-sticker但找不到这个相对简单问题的答案 - 似乎你只能将 MSStickers (不是 StickerViews )添加到 MSStickerBrowserView ,这是显示它们的唯一方法。但是,我需要添加 StickerVIEWS,因为我有一个正在尝试实现的自定义贴纸视图类。
我的贴纸被添加到我的浏览器视图中:
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
}
我尝试将这些方法的返回类型更改为 StickerView 并返回stickerView 数组
func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSStickerView {
return stickerViews[index] //this isnt displaying stickerveiws only stickers
}
然而,这让我得到以下错误:
messagesviewcontroller 不符合协议 msstickerbrowserviewdatasource
因为所需的功能没有像以前那样实现。一个如何显示贴纸视图?我究竟做错了什么?