来自UIView的iOS 14上下文菜单(不是来自UIButton或UIBarButtonItem) [英] iOS 14 Context Menu from UIView (Not from UIButton or UIBarButtonItem)

查看:29
本文介绍了来自UIView的iOS 14上下文菜单(不是来自UIButton或UIBarButtonItem)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种在iOS 13/14中通过UIContextMenuInteraction呈现上下文菜单的简单方法:

anyUIView.addInteraction(UIContextMenuInteraction(delegate: self))

对我来说,这个问题是它模糊了整个用户界面。此外,这只能通过长按/触觉触摸来调用。

如果我不想要模糊效果,可以使用操作菜单。如图所示 https://developer.apple.com/documentation/uikit/menus_and_shortcuts/adopting_menus_and_uiactions_in_your_user_interface

这似乎没有模糊,但似乎只附加到UIButtonUIBarButtonItem

let infoButton = UIButton()
infoButton.showsMenuAsPrimaryAction = true
infoButton.menu = UIMenu(options: .displayInline, children: [])
infoButton.addAction(UIAction { [weak infoButton] (action) in
   infoButton?.menu = infoButton?.menu?.replacingChildren([new items go here...])
}, for: .menuActionTriggered)

是否有方法可以将上下文菜单附加到在长按时调用且不显示模糊的UIView?

推荐答案

经过一些试验后,我能够像这样移除变暗模糊。您需要一个实用程序方法:

extension UIView {
    func subviews<T:UIView>(ofType WhatType:T.Type,
        recursing:Bool = true) -> [T] {
            var result = self.subviews.compactMap {$0 as? T}
            guard recursing else { return result }
            for sub in self.subviews {
                result.append(contentsOf: sub.subviews(ofType:WhatType))
            }
            return result
    }
}

现在,我们使用上下文菜单交互委托方法来查找负责模糊的UIVisualEffectView并将其消除:

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willDisplayMenuFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
    DispatchQueue.main.async {
        let v = self.view.window!.subviews(ofType:UIVisualEffectView.self)
        if let v = v.first {
            v.alpha = 0
        }
    }
}

典型结果:

遗憾的是,菜单后面现在完全没有阴影,但这比大模糊要好。

当然,这仍然是一个漫长的新闻手势。我怀疑对此能做些什么!如果这是一个普通的UILongPressGestureRecognizer,您可能可以找到它并缩短它的minimumPressDuration,但它不是;您必须遵守UIConextMenu交互规则的道路。


话虽如此,如果可能的话,我可以想出一个更好的方法:将这个UIView设置为UIControl!现在,它的行为就像一个UIControl。例如:

class MyControl : UIControl {
    override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let config = UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { _ in
            let act = UIAction(title: "Red") { action in  }
            let act2 = UIAction(title: "Green") { action in  }
            let act3 = UIAction(title: "Blue") { action in  }
            let men = UIMenu(children: [act, act2, act3])
            return men
        })
        return config
    }
}

和:

let v = MyControl()
v.isContextMenuInteractionEnabled = true
v.showsMenuAsPrimaryAction = true
v.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
v.backgroundColor = .red
self.view.addSubview(v)

结果是一个简单的点击即可调出菜单,如下所示:

因此,如果你能摆脱这种做法,我认为这会更好。

这篇关于来自UIView的iOS 14上下文菜单(不是来自UIButton或UIBarButtonItem)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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