Callback for MSSticker Peels in iOS 10 iMessage sticker app

前端 未结 2 963
南方客
南方客 2021-01-31 05:55

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: MSConvers

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 06:28

    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, 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()
    

提交回复
热议问题