好吧,我已经查看了如何以编程方式触发 UIView 的轻击手势识别器和类似的问题,但找不到我要查找的内容。我正在构建一个 iMessage 应用程序扩展,并且需要以编程方式触发 MSSticker 上的点击事件。
我已经使用以下类制作了自定义 MSStickerViews,该类检测何时被按下和/或点击,然后触发另一个功能,但是我不知道如何实际模拟这些点击,以便将贴纸放在输入字段中:
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(stickerView: self)
}
}
func didLongPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == .began {
delegate?.stickerViewDidPeel(stickerView: self)
}
}
}
只调用上面的一个点击函数不起作用的原因是我不需要用点击触发任何特定的函数 - 我只需要在 MSStickerView 中的 MSSticker 上创建一个假点击,这样它就会被放入输入字段。我不知道该怎么做。
我像这样创建我的 MSStickerViews 并将它们放在集合视图中,但是以编程方式点击集合视图单元格没有任何作用(我试过):
var url: URL?
var i = 1
while i < 5 { //while true
url = Bundle.main.url(forResource: "test5", withExtension: "png") //would be "test\(i)"
print("URL IS THIS: \(url)")
guard let url = url else { break }
//make it a sticker
let sticker = try! MSSticker(contentsOfFileURL: url, localizedDescription: "test\(i)")
//let stickerView = InstrumentedStickerView(frame: CGRect(x: 0, y: 0, width: view.bounds.width*0.4, height: view.bounds.width*0.4))
let stickerView = InstrumentedStickerView(frame: CGRect(origin: CGPoint(x: 0,y :0), size: stickerSize))
stickerView.sticker = sticker
stickerView.delegate = self
starterPack.append(stickerView)
i += 1
我有一种感觉可能类似于(伪代码)
starterPack[1].sendActionForEvent(UIEvent.Tap)
是正确的,但 MSStickerView 没有.sendActionForEvent
生无可恋。如何创建假水龙头?