前言
- 默认
UIView
继承了UIResponse
类,通过该类创建UIMenuController
可快捷的实现复制、删除、粘贴等功能; - 可实现的按钮类型包括 剪切、复制、粘贴、选择等,具体可以查看
UIKit
内的UIResponder.h
文件;
重写属性与方法
class CustomView: UIView {
// 使视图具有响应性
override var canBecomeFirstResponder: Bool { true }
override func copy(_ sender: Any?) {
// 复制
}
override func paste(_ sender: Any?) {
// 粘贴
}
override func select(_ sender: Any?) {
// 选择
}
override func delete(_ sender: Any?) {
// 删除
}
}
添加手势事件
extension CustomView {
func setupMenu() {
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(_:)))
addGestureRecognizer(gesture)
}
@objc func longPressAction(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
becomeFirstResponder()
let menu = UIMenuController.shared
menu.setTargetRect(视图.bounds, in: 视图)
menu.setMenuVisible(true, animated: true)
}
}
}
至此就完成了MENU的设置
网友评论