Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback for MSSticker Peels in iOS 10 iMessage sticker app

I'm experimenting with sticker iMessage apps in iOS 10 and I'm running into an issue with the override func didStartSending(_ message: MSMessage, conversation: MSConversation) method in MSMessagesAppViewController. When "peeling" a sticker from an MSStickerView, I would expect to receive some sort of callback on the didStartSending method. But it appears this is not the case. Does anyone know if this is the expected behavior and/or if there's another way to subscribe to callbacks for when these stickers are peeled, dragged, and dropped into the MSConversation? I realize that didStartSending is reserved for when the user taps the send button, but surely there should be some way of knowing when users drag MSStickers without hacking together some UIView dragging/rect-reading heuristic.

Messages 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?
    }

 }

Sticker Browser:

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]
    }

}
like image 536
Andrew Sowers Avatar asked Jul 18 '16 16:07

Andrew Sowers


1 Answers

Here's a subclass and delegate that will tie into the tap and long press gesture recognizers that MSStickerView is using for select and peel interactions. If the implementation of MSStickerView changes this may no longer provide events, but shouldn't crash.

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)
        }
    }
}
like image 122
Hilton Campbell Avatar answered Sep 17 '22 13:09

Hilton Campbell