我在iOS 10中尝试使用贴纸iMessage应用程序,并在MSMessagesAppViewController中遇到覆盖func didStartSending(_ message:MSMessage,conversation:MSConversation)方法的问题.当“剥离”MSStickerView的贴纸时,我会期望在didStartSending方法上收到某种回调.但事实并非如此.有没有人知道这是否是预期的行为,和/或者是否有另一种方式订阅回调,当这些贴纸被剥离,拖动并放入MSConversation?我意识到,didStartSending是为用户点击发送按钮而保留的,但肯定应该有一些方式知道什么时候用户拖动MSStickers,而不会一起盗取一些UIView拖动/直读启发式.
消息视图控制器:
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] } }
这是一个子类和委托,它将绑定到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) } } }