快速自定义上下文菜单预览提供程序无法单击内部的任何视图(使用手势) [英] swift custom context menu previewprovider can not click any view inside(using tapgesture)

查看:58
本文介绍了快速自定义上下文菜单预览提供程序无法单击内部的任何视图(使用手势)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理类似消息的反应,这就是我所做的(使用contextMenu中的customProvider)

im working with reaction like message, here is what i did (using customProvider from contextMenu)

下面是我将表情符号动作添加到customProvider控制器时的代码

below is the code when i added emoji action to customProvider controller

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, y: 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
    }

然后将其添加到自定义提供程序vc:

then i added this to custom provider vc:

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, 0 , 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更改为按钮,但仍然无法点击

But the gesture can not fire, i tried to change img to button, but still not get click

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

我不知道为什么我不能在任何地方单击,即使我试图将手势单击添加到viewcontroller视图.有人可以解释吗?预先感谢

i do not know why i can not click any where, even i tried to add gesture click to viewcontroller view. Can anyone explain it? Thanks in advance

推荐答案

我更改为使用英雄动画呈现新的VC,这很无聊,但这是我目前的最佳选择

i changed to present new VC with hero animation, so bored, but it is my best choice for now

这篇关于快速自定义上下文菜单预览提供程序无法单击内部的任何视图(使用手势)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆