Using touchesended:withevent: Customizing the Copy-and-paste menu, copy: Copying the picture on the screen, cut: Cutting the picture on the screen, paste: pasting the picture from the screen.
1 @implementationPrjcopyandpaste2 3-(void) Viewdidload4 {5 [Super Viewdidload];6 }7 8-(void) Viewwillappear: (BOOL) Animated9 {Ten [Super viewwillappear:animated]; OneSelf.view.backgroundColor =[Uicolor Whitecolor]; A -[Self Setupimagewithname:@"img_01"Center:self.view.center]; -[Self Setupimagewithname:@"img_02"Center:cgpointmake ( $, -)]; the[Self Setupimagewithname:@"img_03"Center:cgpointmake ( -, the)]; - } - --(void) Setupimagewithname: (NSString *) imagenamed Center: (cgpoint) Point + { -Uiimageview *bug =[[Uiimageview alloc]initwithimage:[uiimage imagenamed:imagenamed]]; +Bug.center =Point ; A [Self.view Addsubview:bug]; at } - //become the first responder Show Edit menu --(BOOL) Canbecomefirstresponder - { - returnYES; - } in --(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event to { +Uitouch *touch =[touches anyobject]; - //two consecutive touch display menus the if([Self becomefirstresponder] &&1<[Touch Tapcount]) { *Uimenucontroller *menu =[Uimenucontroller Sharedmenucontroller]; $Self.touchpoint =[Touch LocationInView:self.view];Panax Notoginseng CGRect Minrect; -Minrect.origin =Self.touchpoint; the [Menu Settargetrect:minrect InView:self.view]; + [Menu Setmenuvisible:yes animated:yes]; A } the } + --(BOOL) Canperformaction: (SEL) Action Withsender: (ID) Sender $ { $ if(@selector (copy:) = =action) { - if([self imageContainsPoint:self.touchPoint]) { - returnYES; the } -}Else if(@selector (cut:) = =action) {Wuyi if([self imageContainsPoint:self.touchPoint]) { the returnYES; - } Wu}Else if(@selector (paste:) = =action) { - return(Nil! =[Uipasteboard generalpasteboard].image); About } $ returnNO; - } - --(Uiimageview *) Imagecontainspoint: (cgpoint) Point A { + for(UIView *viewinchself.view.subviews) { the if(Cgrectcontainspoint (View.frame, point)) { - if([View Iskindofclass:[uiimageviewclass]]) { $ return(Uiimageview *) view; the } the } the } the returnNil; - } in-(void) Copy: (ID) Sender the { theUiimageview *imageview =[self imageContainsPoint:self.touchPoint]; About if(ImageView) { the[Uipasteboard generalpasteboard].image =Imageview.image; the } the } +-(void) Cut: (ID) Sender - { theUiimageview *imageview =[self imageContainsPoint:self.touchPoint];Bayi if(ImageView) { the [ImageView Removefromsuperview]; the } - } --(void) Paste: (ID) Sender the { theUipasteboard *pasteboard =[Uipasteboard Generalpasteboard]; the if(pasteboard.image) { theUiimageview *bug =[[Uiimageview alloc] initWithImage:pasteboard.image]; -Bug.center =Self.touchpoint; the [Self.view Addsubview:bug]; thePasteboard.image =Bug.image; the }94 } the the the @end
View Code
Achieve results
Copy/cut/paste pictures in the screen