原来这个组件是UIMenuController

输入图片说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

/**
长按事件
*/
func longPressGestureAction(sender:UILongPressGestureRecognizer){
if sender.state != UIGestureRecognizerState.Began {
return
}
print("longPressGestureAction+++++++++++++++++ \(sender.state.rawValue)")

// 1. first responder
v.becomeFirstResponder()

// 2. 获取菜单
let menu = UIMenuController.sharedMenuController()
if menu.menuVisible == false{
// 3. 设置自定义菜单
let item = UIMenuItem(title: "啦啦啦", action: #selector(self.lalaAction(_:)))
menu.menuItems = [ item ]

// 4. 菜单显示位置
menu.setTargetRect(v.bounds, inView: v)

// 5. 显示菜单
menu.setMenuVisible(true, animated: true)
}
}
func lalaAction(sender:AnyObject?){

}
/// 具备成为第一响应者的资格
override func canBecomeFirstResponder() -> Bool {
return true
}
/**
* 这个方法决定了MenuController的菜单项内容
* 返回YES,就代表MenuController会有action菜单项
*/
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
print("canPerformAction +++++ \(action)")
// 判断 action 中包含的各个事件的方法名称, 对比上了才能显示
if (action == #selector(NSObject.copy(_:)) || action == #selector(self.lalaAction(_:)))
{
return true
}
return false
}

本文标题:原来这个组件是UIMenuController

文章作者:史彦超

发布时间:2016年10月11日 - 22:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2016/10/11/2016-10-11-%E5%8E%9F%E6%9D%A5%E8%BF%99%E4%B8%AA%E7%BB%84%E4%BB%B6%E6%98%AFUIMenuController/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here