UIMenuController simple example (Swift), swift example Daquan

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.