Callback for MSSticker Peels in iOS 10 iMessage sticker app

六月ゝ 毕业季﹏ 提交于 2019-12-02 18:45:00

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

This is a workaround for sticker peeled and tapped events, it is not guaranteed that a particular sticker will be inserted but an approximation of such data points - you will have to use your subclass of MSStickerView. This solution is not futureproof, but works for the time being IMHO, therefore I welcome other ideas.

import UIKit
import Messages

class CustomStickerView : MSStickerView{
    class GestureRecognizerReceiver : NSObject, UIGestureRecognizerDelegate{
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    }
    let _recognizerDelegate = GestureRecognizerReceiver()

    weak var _recognizer: UITapGestureRecognizer? = nil
    func setupTapRecognizer(){
        if _recognizer == nil {
            let r = UITapGestureRecognizer(target: self, action: #selector(_customTapReceived))
            r.cancelsTouchesInView = false
            r.delegate = _recognizerDelegate
            addGestureRecognizer(r)
            _recognizer = r
    }
}

    func _customTapReceived(){
        if let s = sticker{
            Analytics.shared.reportEvent(name: "Sticker Inserted", description: s.localizedDescription)
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let s = sticker{
            Analytics.shared.reportEvent(name: "Sticker Peeled", description: s.localizedDescription)
        }

    }
}

Usage:

let sv = CustomStickerView(frame: _stickerViewHolder.bounds, sticker: sticker)
sv.setupTapRecognizer()
_stickerViewHolder.addSubview(sv)
sv.startAnimating()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!