菜鸟教程小白 发表于 2022-12-12 23:14:27

ios - 如何为 iMessage Sticker App Grid Sticker 提供不同尺寸


                                            <p><p>我创建了一个 iOS iMessage 贴纸应用程序,根据苹果文档,我应该能够以三种不同尺寸之一显示贴纸。 </p>

<p>但是,无论我制作什么尺寸的贴纸图像(300 像素 x 300 像素、408 像素 x 408 像素、618 像素 x 618 像素),它们都只会显示为中间网格选项,每行三个贴纸。有谁知道如何解决这个问题,也许我错过了一些简单的东西? </p>

<p>关于这个主题的文档很少,因为它是相当新的。谢谢您的帮助。 </p>

<p> <img src="/image/hFmcq.jpg" alt="Sticker Sizing Chart"/> </p>

<p>文档链接:<a href="https://developer.apple.com/ios/human-interface-guidelines/extensions/messaging/" rel="noreferrer noopener nofollow">https://developer.apple.com/ios/human-interface-guidelines/extensions/messaging/</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>导入 300x300、408x408 或 618x618 的图像只会影响标签在对话中显示的大小(当作为初始消息发送或拖到现有消息气泡上时。更改图像的尺寸不会影响它的方向网格布局。</p>

<p>要更改 StickerBrowserView 中的网格布局,您可以通过以下两种方式之一进行:</p>

<ul>
<li>界面生成器</li>
</ul>

<p>单击 StickerPack 文件夹使其突出显示(或从您的包中选择任何贴纸),然后您可以从右侧属性检查器的下拉菜单中将“贴纸大小”选项更改为您喜欢的演示文稿样式:小(2 列)、中(3 列)或大(4 列)。就这样!这将更改每个贴纸以在布局中以这种方式显示。无论您以何种尺寸导入图像,您的应用都会将其缩小/放大到正确的尺寸,以便在所选布局中使用。</p>

<p> <a href="/image/j0wtg.png" rel="noreferrer noopener nofollow"><img src="/image/j0wtg.png" alt="Change Sticker Size from Interface Builder"/></a> </p>

<ul>
<li>以编程方式</li>
</ul>

<p>首先,将一个新项目设置为“消息应用程序”,然后创建一个新文件来继承 MSStickerBrowserViewController 的子类,创建数组来存储贴纸,加载/创建贴纸(同时将每个贴纸存储到贴纸数组中),并确保实现MSStickerBrowserViewController 的 2 个必需的数据源方法。为此,您只需将图像拖到 Extensions 目录中即可。</p>

<p>子类文件.Swift</p>

<pre><code>import UIKit
import Messages

class MyBrowserVC: MSStickerBrowserViewController {

    //create stickers array
    var stickers = ()

    //load assets into stickers array
    func loadStickers() {
      createSticker(asset: &#34;boycott&#34;, localizedDescription: &#34;boycottSticker&#34;)
      createSticker(asset: &#34;alluminaughty&#34;, localizedDescription: &#34;alluminaughtySticker&#34;)
      createSticker(asset: &#34;beer&#34;, localizedDescription: &#34;beerSticker&#34;)
    }

    //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: &#34;png&#34;) else {
            print(&#34;Couldn&#39;t create sticker path for&#34;, 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) -&gt; Int {
      return stickers.count
    }
    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView,
                                          stickerAt index: Int) -&gt; MSSticker {
      return stickers
    }
}
</code></pre>

<p>在您的 MessagesViewController(开始新项目时应该自动出现)中,从您的子类创建 BrowserVC 的实例,设置贴纸大小,browserVC 框架,将 subview 添加到前面,并将贴纸加载到浏览器 View 。</p >

<p>MessageViewController.swift</p>

<pre><code>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 &#34;grid layout&#34;
    //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()
    }
}
</code></pre>

<p>如需了解更多信息,请查看 <a href="https://developer.apple.com/videos/play/wwdc2016/204/" rel="noreferrer noopener nofollow">WWDC video</a>去年在贴纸上发布!这是我开始学习制作贴纸的地方,然后查阅了文档!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何为 iMessage Sticker App Grid Sticker 提供不同尺寸,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41179528/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41179528/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何为 iMessage Sticker App Grid Sticker 提供不同尺寸