IOS IM development suggestions (4) How to Use UIMenuController, iosuimenucontroller

Source: Internet
Author: User

IOS IM development suggestions (4) How to Use UIMenuController, iosuimenucontroller

This should be the last article in this series. I did not write a lot, but if I did not write it, it would never be good. You are welcome to contact us for error correction.

 

UIMenuController, which is not as unfriendly as Alert or ActionSheet, must be clicked. You can ignore its appearance or call some set methods through it. The length is as follows:

  

This control is not very familiar. You can press it to display a view. If you do IM, wechat is the benchmark, so how can you let it go.

First, design a common scenario: on the chat interface, long press textView, then the menu is displayed, and then you click an Item.

Chat page: tableView + inputBar (a view with an input box ).

Here you will use the tableView proxy to implement several delegate.

1 # pragma mark-UITableViewDataSource & UITableViewDelegate 2-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {3 // how many pieces of data are there 4 return [dataArray count]; 5} 6 7-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {8 static NSString * CellIdentifier = @ "MessageCell "; 9 MessageCell * cell = [tableView failed: CellIdentifier]; 10 if (cell = nil) {11 cell = [MessageCell alloc] initWithStyle: descrireuseidentifier: CellIdentifier]; 12 cell. selectionStyle = UITableViewCellSelectionStyleDefault; 13} 14 MessageModel * model = [dataArray objectAtIndex: indexPath. row]; 15 [cell setMyContent: model]; 16 return cell; 17} 18 19-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {20 // here the previously written height is used to calculate 21 return # cellHeightFromModelData #; 22}

Let's take a look at cell. cell has to do a few things: 1. Calculate its own frame; 2. Set textView or imageView. Here I just want to talk about textView;

. H. We need to declare a public method and use model to set the cell to reduce the amount of VC code. There is also a block that returns textView Interaction Events to VC.

 1 typedef NS_ENUM(NSInteger,CellOperations) { 2     TextCopy, 3     TextDelete, 4     TextTransfor, 5     TextCollection, 6  7     ImageCopy, 8     ImageDelete, 9     ImageTransfor,10     ImageCollection,11 };12 13 @property(nonatomic,copy)void (^CellOperation)(MessageModel* message, CellOperations cop);14 15 - (void)setMyContent:(MessageModel *)message;

. M we need to calculate our own height, textView height. There are also some events that pass textView to VC.

1-(void) setMyContent :( MessageModel *) message {2 // Pass Parameter 3 // calculate height 4 // set textView size and background frame size 5} 6 7-(void) textViewCallBack :( CellOperations cop) {8 // capture textView operations 9 // call your callBackVC to pass the operation 10} 11 12-(void) callBackVC :( CellOperations cop) {13 // transfer operation event 14 to VC}

To implement the above three methods, we can draw a Cell and pass the textView operation.

Next, textView.

. H here we need to declare a Block to pass the value to the Cell.

@property(nonatomic,copy)void (^TVOperation)(CellOperations cop);

. M. This is the implementation of UIMenuController.

1 // 2 // DisplayTextView. m 3 // 4 // 5 // Created by akforsure on 15/12/1. 6 // Copyright©2015 akforsure. all rights reserved. 7 // 8 9 # import "DisplayTextView. h "10 11 @ implementation DisplayTextView {12 UIMenuController * menu; 13} 14-(instancetype) initWithFrame :( CGRect) frame {15 self = [super initWithFrame: frame]; 16 if (self) {17 // The textView used for display must not be edited or selected, otherwise the very second 18 self. editable = NO; 19 self. selectable = NO; 20} 21 return self; 22} 23-(void) showMenu {24 // if it already exists, return 25 if ([menu isMenuVisible]) return; 26 // If textView is the first response, it must be set to the first response 27 [self becomeFirstResponder]; 28 // set UIMenuItems and add it to the page 29 UIMenuItem * menuItem0 = [[UIMenuItem alloc] initWithTitle: @ "copy" action: @ selector (kCopy :)]; 30 optional * menuItem1 = [[UIMenuItem alloc] initWithTitle: @ "delete" action: @ selector (kDelete :)]; 31 UIMenuItem * menuItem2 = [[UIMenuItem alloc] initWithTitle: @ "Forward" action: @ selector (transfor :)]; 32 UIMenuItem * menuItem3 = [[UIMenuItem alloc] initWithTitle: @ "add to Favorites" action: @ selector (collection :)]; 33 menu = [UIMenuController sharedMenuController]; 34 [menu setMenuItems: [NSArray items: menuItem0, menuItem1, menuItem2, menuItem3, nil]; 35 [menu setTargetRect: self. bounds inView: self]; 36 [menu setMenuVisible: YES animated: YES]; 37} 38 // This method must be implemented otherwise you cannot respond to your action 39-(BOOL) canBecomeFirstResponder {40 return YES; 41} 42 // This method returns YES to the response to the click, that is, it can be recognized 43-(BOOL) can1_maction :( SEL) action withSender :( id) sender {44 45 if (action = @ selector (kCopy :)) {46 return YES; 47} else if (action = @ selector (kDelete :)) {48 return YES; 49} else if (action = @ selector (transfor :)) {50 return YES; 51} 52 else if (action = @ selector (collection :)) {53 return YES; 54} 55 [super can1_maction: action withSender: sender]; 56 return NO; 57} 58 # pr1_mark-private methods implementation of each event 59-(void) kCopy :( id) sender {60 if (self. TVOperation) {61 self. TVOperation (TextCopy); 62} 63 NSLog (@ "textkCopy"); 64} 65-(void) kDelete :( id) sender {66 if (self. TVOperation) {67 self. TVOperation (TextDelete); 68} 69 NSLog (@ "textkDelete"); 70} 71-(void) transfor :( id) sender {72 if (self. TVOperation) {73 self. TVOperation (TextTransfor); 74} 75 NSLog (@ "texttransfor"); 76} 77-(void) collection :( id) sender {78 if (self. TVOperation) {79 self. TVOperation (TextCollection); 80} 81 NSLog (@ "textcollection"); 82} 83 84 85 // Only override drawRect: if you perform custom drawing. 86 // An empty implementation adversely affects performance during animation. 87 // Add the long press gesture 88-(void) drawRect (CGRect) to the draw) rect {89 // Drawing code 90 // remove long-pressed gesture 91 for (UIGestureRecognizer * recognizer in self. gestureRecognizers) {92 if ([recognizer isKindOfClass: [UILongPressGestureRecognizer class]) {93 recognizer. enabled = NO; 94 // there may be a problem, i'm not sure, but it took some time to get 95} 96} 97 // re-join the long-pressed gesture 98 UILongPressGestureRecognizer * kGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (showMenu)]; 99 [self addGestureRecognizer: kGesture]; 100} 101 102 @ end

So far, we have finished the effect you have seen. I didn't give all the code, because I hope everyone can write it by themselves.

 

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.