This is a very useful column Alias tool class, in the Chat dialog box to add and QQ the same "copy, Paste, Cancel" and other options, and easy to use, as long as you find a chat bubble inside a view, such as label or background bubbling uiimageview, directly add ... You can achieve the following effects:
Directly on the source:
Uiview+pressmenu.h
1 #import<UIKit/UIKit.h>2 3 @interfaceUIView (Pressmenu)4@property (Strong, nonatomic) Nsarray *Menutitles;5@property (Strong, nonatomic) Uilongpressgesturerecognizer *PRESSGR;6@property (copy, nonatomic)void(^menuclickedblock) (Nsinteger index, NSString *title);7@property (Strong, nonatomic) Uimenucontroller *MENUVC;8 9- (void) Addpressmenutitles: (Nsarray *) menutitles Menuclickedblock: (void(^) (Nsinteger index, NSString *title)) Block;Ten- (void) Showmenutitles: (Nsarray *) menutitles Menuclickedblock: (void(^) (Nsinteger index, NSString *title)) Block; One A-(BOOL) ismenuvcvisible; -- (void) Removepressmenu; - @end
uiview+pressmenu.m
1 #import "uiview+pressmenu.h"2 #import<objc/runtime.h>3 4 @implementationUIView (Pressmenu)5 6 Static ConstNSString *kpressmenuselectorprefix =@"Easepressmenuclicked_";7 Static CharPressmenutitleskey, Pressmenublockkey, Pressmenugesturekey, Menuvckey;8 9 #pragmaMark MTen- (void) Addpressmenutitles: (Nsarray *) menutitles Menuclickedblock: (void(^) (Nsinteger index, NSString *title)) block{ Oneself.userinteractionenabled =YES; ASelf.menuclickedblock =Block; -Self.menutitles =Menutitles; - if(SELF.PRESSGR = =Nil) { theSELF.PRESSGR =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (handlepress:)]; - } - [self AddGestureRecognizer:self.pressGR]; - } + -- (void) Showmenutitles: (Nsarray *) menutitles Menuclickedblock: (void(^) (Nsinteger index, NSString *title)) block{ +Self.menuclickedblock =Block; ASelf.menutitles =Menutitles; at [self p_showmenu]; - } - --(BOOL) ismenuvcvisible{ - if(SELF.MENUVC) { - return[SELF.MENUVC ismenuvisible]; in } - returnNO; to } + -- (void) removepressmenu{ the if(SELF.MENUVC) { * [SELF.MENUVC setmenuvisible:no animated:yes]; $SELF.MENUVC =Nil;Panax Notoginseng } - if([Self.pressgr Iskindofclass:[uilongpressgesturerecognizerclass]]) { the [self RemoveGestureRecognizer:self.pressGR]; +SELF.PRESSGR =Nil; A } the if(self.menuclickedblock) { +Self.menuclickedblock =Nil; - } $ if(self.menutitles) { $Self.menutitles =Nil; - } - } the - #pragmaMark Set_getWuyi- (void) Setmenutitles: (Nsarray *) menutitles{ theObjc_setassociatedobject (Self, &Pressmenutitleskey, Menutitles, objc_association_retain_nonatomic); - } Wu-(Nsarray *) menutitles{ - returnObjc_getassociatedobject (Self, &pressmenutitleskey); About } $ -- (void) Setmenuclickedblock: (void(^) (Nsinteger, NSString *)) menuclickedblock{ -Objc_setassociatedobject (Self, &Pressmenublockkey, Menuclickedblock, objc_association_copy_nonatomic); - } A- (void(^) (Nsinteger, NSString *)) menuclickedblock{ + returnObjc_getassociatedobject (Self, &pressmenublockkey); the } - $- (void) SETPRESSGR: (Uilongpressgesturerecognizer *) pressgr{ theObjc_setassociatedobject (Self, &Pressmenugesturekey, PRESSGR, objc_association_retain_nonatomic); the } the-(Uilongpressgesturerecognizer *) pressgr{ the returnObjc_getassociatedobject (Self, &pressmenugesturekey); - } in the- (void) SETMENUVC: (Uimenucontroller *) menuvc{ theObjc_setassociatedobject (Self, &Menuvckey, MENUVC, objc_association_retain_nonatomic); About } the-(Uimenucontroller *) menuvc{ the returnObjc_getassociatedobject (Self, &menuvckey); the } + - #pragmaMark Canperformaction the-(BOOL) canbecomefirstresponder{Bayi if(self.menuclickedblock) { the returnYES; the}Else{ - return[Super Canbecomefirstresponder]; - } the } the the-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{ the if(self.menuclickedblock) { - for(intI=0; i<self.menutitles.count; i++) { the if(Action = = nsselectorfromstring ([NSString stringWithFormat:@"%@%d:", Kpressmenuselectorprefix, I])) { the returnYES; the }94 } the returnNO; the}Else{ the return[Super Canperformaction:action Withsender:sender];98 } About } - 101-(void) Handlepress: (uigesturerecognizer*) recognizer{102 if(Recognizer.state = =Uigesturerecognizerstatebegan) {103 [self p_showmenu];104 } the }106 107- (void) p_showmenu{108 [self becomefirstresponder];109Nsmutablearray *menuitems =[[Nsmutablearray alloc] initWithCapacity:self.menuTitles.count]; theClass CLS = [selfclass];111SEL imp =@selector (pressmenuclicked:); the for(intI=0; i<self.menutitles.count; i++) {113NSString *title =[Self.menutitles objectatindex:i]; the //register Name Add method Sel,sel specific implementation in IMP (pressmenuclicked:) theSel sel = Sel_registername ([[NSString stringWithFormat:@"%@%d:", Kpressmenuselectorprefix, I] Utf8string]); theClass_addmethod (CLS, SEL, [CLS Instancemethodforselector:imp],"[email protected]");117UIMenuItem *menuitem =[[UIMenuItem alloc] Initwithtitle:title Action:sel];118 [MenuItems Addobject:menuitem];119 } -Uimenucontroller *menu =[Uimenucontroller Sharedmenucontroller];121 [Menu Setmenuitems:menuitems];122 [Menu SetTargetRect:self.frame InView:self.superview];123 [Menu Setmenuvisible:yes animated:yes];124SELF.MENUVC =menu; the }126 127- (void) Pressmenuclicked: (ID) Sender { -NSString *selstr =Nsstringfromselector (_cmd);129NSString *indexstr =[Selstr substringFromIndex:kPressMenuSelectorPrefix.length]; theNsinteger index =Indexstr.integervalue;131 if(Index >=0&& index<self.menuTitles.count) { theNSString *title =[Self.menutitles Objectatindex:index];133 if(self.menuclickedblock) {134 Self.menuclickedblock (index, title);135 }136 }137 }138 139 @end
iOS Development Function module--use runtime to extend the Pressmenu tool to the UIView category