如何解决swift 自定义上下文菜单 previewprovider 无法单击内部的任何视图使用 tapgesture
我正在处理类似消息的反应,这就是我所做的(使用来自 contextMenu 的 customProvider)
下面是我向 customProvider 控制器添加表情动作时的代码
private func addReactionList(_ items : [Reaction]) -> UIView{
let reactionView = UIView()
var arrangedSubviews: [UIView] = []
for reaction in items {
let imgView = UIImageView(image: AppUtils.reactionToImage(reaction: reaction))
imgView.isUserInteractionEnabled = true
imgView.frame = CGRect(x: 0,y: 0,width: iconHeight,height: iconHeight)
imgView.layer.cornerRadius = (imgView.frame.height) / 2
imgView.layer.masksToBounds = true
imgView.layer.borderColor = UIColor.clear.cgColor
imgView.layer.borderWidth = 0
imgView.contentMode = .scaleAspectFit
imgView.restorationIdentifier = reaction.Type
let tapGesture = UITapGestureRecognizer(target: self,action: #selector(handleTapGesture(_:)))
tapGesture.delegate = self
tapGesture.numberOfTapsrequired = 1
tapGesture.delaystouchesBegan = true
tapGesture.cancelstouchesInView = false
imgView.isUserInteractionEnabled = true
imgView.addGestureRecognizer(tapGesture)
arrangedSubviews.append(imgView)
}
let stackView = UIStackView(arrangedSubviews: arrangedSubviews)
stackView.distribution = .fillEqually
stackView.spacing = padding
stackView.layoutMargins = UIEdgeInsets(top: padding,left: padding,bottom: padding,right: padding)
stackView.isLayoutMarginsRelativeArrangement = true
let width = (CGFloat(items.count) * iconHeight) + (CGFloat(items.count+1) * padding)
reactionView.frame = CGRect(x: 0,width: width,height: iconHeight + 2 * padding)
reactionView.layer.cornerRadius = 12
reactionView.addSubview(stackView)
stackView.frame = reactionView.frame
stackView.isUserInteractionEnabled = false
reactionView.isUserInteractionEnabled = false
return reactionView
}
let actionsReacts = addReactionList(likeReactions)
let emojiReacts = addReactionList(emojiReactions)
if let snap = customView.snapshotView(afterScreenUpdates: true) as UIView?{
view.addSubview(snap)
view.addSubview(actionsReacts)
view.addSubview(emojiReacts)
let maxWidth = snap.frame.size.width >= actionsReacts.frame.size.width ? snap.frame.size.width : actionsReacts.frame.size.width
var currentSnapFrame = snap.frame
currentSnapFrame = CGRect(Int((maxWidth - snap.frame.size.width)) / 2,Int(snap.frame.size.width),Int(snap.frame.size.height))
snap.frame = currentSnapFrame
var currentActionsReactFrame = actionsReacts.frame
currentActionsReactFrame = CGRect(0,Int(snap.frame.size.height),Int(actionsReacts.frame.size.width),Int(actionsReacts.frame.size.height))
actionsReacts.frame = currentActionsReactFrame
var currentEmojiReactFrame = actionsReacts.frame
currentEmojiReactFrame = CGRect(0,Int(snap.frame.size.height) + Int(emojiReacts.frame.size.height),Int(emojiReacts.frame.size.width),Int(emojiReacts.frame.size.height))
emojiReacts.frame = currentEmojiReactFrame
preferredContentSize = CGSize(width: maxWidth,height: snap.frame.size.height + actionsReacts.frame.size.height + emojiReacts.frame.size.height)
}
但手势无法触发,我尝试将 img 更改为按钮,但仍然无法点击
@objc func handleTapGesture(_ sender : UITapGestureRecognizer){
let tappedImage = sender.view as! UIImageView
let reactionType = tappedImage.restorationIdentifier ?? ""
onSelectedReaction?(reactionType)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,shouldReceive touch: UITouch) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我不知道为什么我不能点击任何地方,即使我试图将手势点击添加到视图控制器视图。谁能解释一下?提前致谢
解决方法
我改用英雄动画来呈现新的VC,好无聊,但这是我现在最好的选择
,我尝试根据您提供的内容重建您的演示应用程序,但我仍然不清楚您的代码是什么或其行为方式。
我注意到您正在创建 n
手势识别器并将每个识别器连接到图像视图。
尝试改用 1 个手势识别器。将其连接到您的父视图,并在点击事件上获取正在被点击的子视图。
一些代码:
let tap = UITapGestureRecognizer(target: self,action: #selector(self.onViewTap))
parentView.addGestureRecognizer(tap)
@objc func onViewTap(_ sender: UITapGestureRecognizer) {
let point = sender.location(in: containerView)
if let hitSubview = parentView.hitTest(point,with: nil) {
// check which subview (imageView) is being hit by checking its tag or similar
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。