我正在尝试在iOS 10中使用贴纸iMessage应用程序,但是override func didStartSending(_ message: MSMessage, conversation: MSConversation)中的MSMessagesAppViewController方法遇到问题。当从MSStickerView“剥离”标签时,我希望在didStartSending方法上收到某种回调。但似乎并非如此。有谁知道这是否是预期的行为,并且/或者是否有其他方法可以在这些标签被剥离,拖动和放入MSConversation中时订阅回调?我意识到didStartSending是在用户点击“发送”按钮时保留的,但可以肯定的是,应该有某种方式知道用户何时拖动MSStickers,而不会共同掌握一些UIView拖动/正读启发式方法。

消息 View Controller :

class MessagesViewController: MSMessagesAppViewController {

    var nYCStickersBroswerViewController: NYCStickersBroswerViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        nYCStickersBroswerViewController = NYCStickersBroswerViewController(stickerSize: .regular)
        nYCStickersBroswerViewController.view.frame = self.view.frame

        self.addChildViewController(nYCStickersBroswerViewController)
        nYCStickersBroswerViewController.didMove(toParentViewController: self)
        self.view.addSubview(nYCStickersBroswerViewController.view)

        nYCStickersBroswerViewController.loadStickers()
        nYCStickersBroswerViewController.stickerBrowserView.reloadData()
    }

    ...

    override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
        // Called when the user taps the send button.
        print(message) // should this not contain the sticker that is peeled, dragged, and dropped into the conversation?
    }

 }

贴纸浏览器:
import Foundation
import UIKit
import Messages

class ASSticker: MSSticker {
    var identifier: String?
}

class NYCStickersBroswerViewController: MSStickerBrowserViewController {

    var stickers = [ASSticker]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func changeBrowswerViewBackgroundColor(color: UIColor) {
        stickerBrowserView.backgroundColor = color
    }

    func loadStickers() {
        createSticker(name: "brooklyn", localizedDescription: "Brooklyn Bridge Sticker")
        createSticker(name: "liberty", localizedDescription: "Statue of Liberty Sticker")
        createSticker(name: "love", localizedDescription: "I Love New York Sticker")
        createSticker(name: "mets", localizedDescription: "New York Mets Sticker")
        createSticker(name: "rangers", localizedDescription: "New York Rangers Sticker")
        createSticker(name: "subway", localizedDescription: "New York City MTA Subway Train Sticker")
    }

    func createSticker(name: String, localizedDescription: String) {
        guard let stickerPath = Bundle.main.pathForResource(name, ofType: "png") else {
            print("Call ae cab, you're intoxicated.")
            return
        }
        let stickerURL = URL(fileURLWithPath: stickerPath)
        let sticker: ASSticker
        do {
            try sticker = ASSticker(contentsOfFileURL: stickerURL, localizedDescription: localizedDescription)
            sticker.identifier = "something unique"
            stickers.append(sticker)
        } catch {
            print("Call a cab, you're intoxicated.")
        }
    }

    override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
        return self.stickers.count
    }
    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
        return self.stickers[index]
    }

}

最佳答案

这是一个子类和委托(delegate),将与MSStickerView用于选择和剥离交互的敲击和长按手势识别器相关联。如果MSStickerView的实现发生更改,它可能不再提供事件,但不应崩溃。

import UIKit
import Messages

protocol InstrumentedStickerViewDelegate: class {
    func stickerViewDidSelect(stickerView: MSStickerView)
    func stickerViewDidPeel(stickerView: MSStickerView)
}

class InstrumentedStickerView: MSStickerView {
    weak var delegate: InstrumentedStickerViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)

        for gestureRecognizer in gestureRecognizers ?? [] {
            if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer {
                tapGestureRecognizer.addTarget(self, action: #selector(didTap))
            } else if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer {
                longPressGestureRecognizer.addTarget(self, action: #selector(didLongPress))
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func didTap(tapGestureRecognizer: UITapGestureRecognizer) {
        if tapGestureRecognizer.state == .Recognized {
            delegate?.stickerViewDidSelect(self)
        }
    }

    func didLongPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
        if longPressGestureRecognizer.state == .Began {
            delegate?.stickerViewDidPeel(self)
        }
    }
}

07-26 09:32