UIMenuController simple example (Swift), swift example Daquan
The first time I got started with Swift, I felt a little simpler, but I was not used to it. I didn't look at the basic knowledge. I went straight to the demo!
You can compare it with the steps in http://www.cnblogs.com/hissia/p/5668513.html, which is easy to understand.
Directly run the Code:
1 import UIKit 2 3 class ZWLabel: UILabel {4 5 // MARK: drag a UILabel directly in the storyboard, and set the class to ZWLabel 6 override func awakeFromNib () 7 {8 // user interaction 9 self. userInteractionEnabled = true10 11 // bind gesture 12 self. addGestureRecognizer (UITapGestureRecognizer. init (target: self, action: # selector (ZWLabel. labelClick) 13} 14 func labelClick () 15 {16 // lable became the first responder 17 self. becomeFirstResponder () 18 19 // get menu 20 let menu = UIMenuController. sharedMenuController () 21 22 if menu. menuVisible {23 menu. setMenuVisible (false, animated: true) 24} else {25 // set custom menu 26 menu. menuItems = [UIMenuItem. init (title: "copy", action: # selector (ZWLabel. test (_ :))] 27 28 // menu display position 29 menu. setTargetRect (self. bounds, inView: self) 30 31 // display menu 32 menu. setMenuVisible (true, animated: true) 33} 34} 35 36 // MARK: Try 37 func test (menu: UIMenuContro Roller) 38 {39 print ("try") 40} 41 42 // MARK: Give Lable the qualification to be the first responder 43 override func canBecomeFirstResponder () -> Bool44 {45 return true46} 47 48 // MARK: return option 49 override func can1_maction (action: Selector, withSender sender: AnyObject?) displayed in the floating menu ?) -> Bool50 {51 // determines the method name of each event contained in the action. After comparison, 52 if (action = # selector (NSObject. copy (_ :) | action ==# selector (ZWLabel. test (_ :)) 53 {54 return true55} 56 return false57} 58 59 override func copy (sender: AnyObject ?) {60 print ("Try copying") 61} 62 63}