导入 300x300、408x408 或 618x618 的图像只会影响标签在对话中显示的大小(当作为初始消息发送或拖到现有消息气泡上时。更改图像的尺寸不会影响它在网格布局中的方向.
要更改 StickerBrowserView 中的网格布局,您可以使用以下两种方法之一:
单击 StickerPack 文件夹,使其突出显示(或从包中选择任何贴纸),然后您可以从右侧属性检查器的下拉菜单中将“贴纸大小”选项更改为您喜欢的演示样式:小(2 列)、中(3 列)或大(4 列)。就这样!这将更改每个贴纸以在布局中以这种方式显示。无论您以何种尺寸导入图像,您的应用程序都会将其缩小/放大到正确的尺寸,以便在所选布局中使用。
首先,将一个新项目设置为“消息应用程序”,然后创建一个新文件以继承 MSStickerBrowserViewController,创建数组来存储贴纸,加载/创建贴纸(同时将每个贴纸存储到贴纸数组中),并确保实现所需的 2 MSStickerBrowserViewController 的数据源方法。为此,您只需将图像拖到 Extensions 目录中即可。
子类文件.Swift
import UIKit
import Messages
class MyBrowserVC: MSStickerBrowserViewController {
//create stickers array
var stickers = [MSSticker]()
//load assets into stickers array
func loadStickers() {
createSticker(asset: "boycott", localizedDescription: "boycottSticker")
createSticker(asset: "alluminaughty", localizedDescription: "alluminaughtySticker")
createSticker(asset: "beer", localizedDescription: "beerSticker")
}
//func to create sticker
func createSticker(asset: String, localizedDescription: String) {
//create url from assets in main bundle
guard let stickerPath = Bundle.main.path(forResource: asset, ofType: "png") else {
print("Couldn't create sticker path for", asset)
return
}
let stickerURL = URL(fileURLWithPath: stickerPath)
let sticker: MSSticker
//create sticker from path(and localized description) and add to array
do {
try sticker = MSSticker(contentsOfFileURL: stickerURL,
localizedDescription: localizedDescription)
stickers.append(sticker)
} catch {
print(error)
return
}
}
//datasource methods
override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
return stickers.count
}
override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView,
stickerAt index: Int) -> MSSticker {
return stickers[index]
}
}
在您的 MessagesViewController(开始新项目时应该自动出现)中,从您的子类创建 BrowserVC 的实例,设置贴纸大小,browserVC 框架,将子视图添加到前面,并将贴纸加载到浏览器视图。
MessageViewController.swift
import UIKit
import Messages
class MessagesViewController: MSMessagesAppViewController {
//create BrowserVC instance of Subclass
var browserVC: MyBrowserVC!
override func viewDidLoad() {
super.viewDidLoad()
//the next line of code is where you can adjust the "grid layout"
//your options are: .small(2 columns), .regular(3 columns), or .large(4 columns)
//instantiate browserVC with sticker size and set frame
browserVC = MyBrowserVC(stickerSize: .regular)
browserVC.view.frame = self.view.frame
//send browserVC to front
self.addChild(browserVC)
browserVC.didMove(toParent: self)
self.view.addSubview(browserVC.view)
//load stickers onto the browser view
browserVC.loadStickers()
browserVC.stickerBrowserView.reloadData()
}
}
如需了解更多信息,请查看去年在 Stickers 上发布的WWDC 视频!这是我开始学习制作贴纸的地方,然后查阅了文档!