Callback for MSSticker Peels in iOS 10 iMessage sticker app

前端 未结 2 961
南方客
南方客 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:20

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

提交回复
热议问题